Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2012, 07:24 PM   PM User | #1
hcrosex3
New Coder

 
Join Date: Sep 2012
Posts: 32
Thanks: 10
Thanked 0 Times in 0 Posts
hcrosex3 is an unknown quantity at this point
Reformat number after input.

I'm trying you have the number entered be reformatted to add dashes after validation. But it is not working. Any suggestions?
Code:
 function validateField ()
{
	var rgEX = /^(\d{2}[-]?)(\d{2}[-]?)(\d{3})$/;
        if ( ! rgEX.test( this.value ) )
        {
        	this.value = '';
 		alert("not a vln");
	} else {
    this.value = str.replace(this.value,' $1- $2- $3')
}
hcrosex3 is offline   Reply With Quote
Old 12-03-2012, 08:15 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,058 Times in 4,027 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, since you made the dashes optional in the RegExp, I would do it this way:
Code:
 function validateField ()
{
    var val = this.value.replace(/\D/g, "" ); // zap all NON-digit characters
    if ( val.length != 7 )  /* then there must be 7 digits exactly */
    {
        this.value = '';
 	alert("not a vln");
        return false; // ?? may not be needed, can't hurt
    } 
    this.value = val.substr(0,2) + "-" + val.substr(2,2) + "-" + val.substr(4);
    return true;
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:12 PM.


Advertisement
Log in to turn off these ads.