...

View Full Version : Reformat number after input.



hcrosex3
12-03-2012, 07:24 PM
I'm trying you have the number entered be reformatted to add dashes after validation. But it is not working. Any suggestions?

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')
}

Old Pedant
12-03-2012, 08:15 PM
Well, since you made the dashes optional in the RegExp, I would do it this way:


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;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum