Check if string is valid SSN as its being typed using Javascript -


i want validate check see if ssn entered textbox follows specific format: xxx-xx-xxxx being typed

i tried doing function auto formats this...

function ssncheck() {     var val = this.value.replace(/\d/g, '');     var newval = '';     if(val.length > 4) {         this.value = val;     }     if((val.length > 3) && (val.length < 6)) {         newval += val.substr(0, 3) + '-';         val = val.substr(3);     }     if (val.length > 5) {         newval += val.substr(0, 3) + '-';         newval += val.substr(3, 2) + '-';         val = val.substr(5);     }      newval += val;     this.value = newval; } 

but bug out , not work on mobile devices. tried solutions below, aren't working. way i'm applying above doing this..

document.getelementbyid("ssn").addeventlistener('keyup',ssncheck,false); 

any on getting work auto formats being typed, or better solution issue since has show in xxx-xx-xxxx format when done typing in box.

try regax:

^\d{3}(?:[-]\d{2})(?:[-]\d{4})?$ 
  • ^ = start of string.
  • (?:…) = grouping
  • \d{3} = match 3 digits
  • \d{2} = match 2 digits
  • \d{4} = match 4 digits
  • [-] = match hyphen
  • $ = end of string

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -