Click here to Skip to main content
15,903,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I needed a javascript function that would pattern match a text value for 9 digit num. Within the function, i do land up in the else part and get the message "A Number" but then receive a FALSE for the next validation, when i enter a 9 digit number say 123456789. I get FALSE even with regSSN1.match(textValue). Where am i going wrong?
JavaScript
function validateFor9DigitNumber() {
  var txt = document.getElementById('<%= txt9Digit.ClientId %>');
  var textValue = parseInt(txt.value);
  var regSSN1 = new RegExp("^\d{9}$");
  if (isNaN(textValue)){
    alert("Not a Number");
  } else {
    alert("A Number");
    alert(regSSN1.test(textValue));
  }
}
Posted
Updated 23-Mar-11 21:27pm
v3
Comments
Eduard Keilholz 24-Mar-11 3:28am    
Styled the question a little to be able to read it better.

1 solution

hello,

try using literal syntax

var regSSN1 = /^\d{9}$/;

Make sure to use ClientID instead of ClientId. ID is uppercase.

var txt = document.getElementById('<%= txt9Digit.ClientID %>');


Source:
http://www.javascriptkit.com/javatutors/re.shtml
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900