Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating number and comma

Status
Not open for further replies.

Maximus007

Technical User
Jul 26, 2004
248
0
0
US
I have an input field that populates a comma after the third number:

code:
<input type="text" onkeyup="this.value = (this.value.length == 3) ? this.value + ',' : this.value">

in my validating how can I allow the comma and have check for that number only.


code:
if (isNaN(dd.value)) {rdd.innerText = 'Please enter 6-digit number'; return false;}
 
You could use a regular expression to evaluate the number but the above method leaves you open for other possible issues.
If the person types in 111,111 where THEY type in the comma your script is still attempting to enter the comma which displaces one of their digits so you end up with 111,,1.

My suggestion is to not automatically insert the comma.
When you validate read the value of the field, strip out any non-numerical characters, verify it is a 6 digit number and then insert the comma and write it back to the field.

Code:
function testVal(which) {
  var tempNum = stripNonNum(which.value);
  if (tempNum.length == 6) 
    which.value = tempNum.substring(0,3) + ',' + tempNum.substring(3,6);
  else
    alert('Please enter 6-digit number');
}

function stripNonNum(value) {
  return value.replace(/[^0-9]/g,'');
}

You CAN do what you want with a regular expression though I do not have one to validate a number with commas at the moment but you would have to do additional tests to ensure they did not put their own comma in in a different position.



Stamp out, eliminate and abolish redundancy!
 
Sorry for the late response but thank you for helping me. The way you explained made it clearer and I was able to make the necessary changes to get it working.

Ralph
 
Glad it worked for you.
I wish I could have answered the question you asked directly. I personally dislike when I ask a specific question and the only response is "don't do it that way do it this way..."
I do not know regular expressions well enough to come up with one that would validate the field directly though I am sure given time I could come up with one. But I did see that you would then end up with the possibility that the user would type in their own comma making the validation a lot more complex.

I am working on my own auto-validation system and part of it will include conversion of input data into a masked output so that a phone number like: 555-555-4444 will validate but then be put back as (555) 555-4444 so the end result will always be in the format I want but still allow the end-user to enter the data however they may whether they use parens, spaces, periods or hyphens as long as they have the right number of digits it will still work. Same will be true with credit card numbers, Social Sec numbers, etc...


Paranoid? ME?? Who wants to know????
 
Just an idea theniteowl;

You might want to check for, and remove, a leading "1" in your phone number validation script...took me a lot of headaches to figure out why a couple of my clients couldn't get their numbers to validate once.... [hourglass]




I hope this helps;
Rob Hercules
 
Thanks robherc, I do that when it is a U.S. number but I have not determined if I should for international numbers yet. Of course I have not written any forms that would be used international so...

I also test that the first digit of the areacode AND the exchange do not begin with 1 as there are no areacodes or exchanges beginning with that number.


Paranoid? ME?? Who wants to know????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top