javascript - Validate specific format without using "-" to split the string -


hi need validate string before sending form, format must be

llll999999xxx

where l letter 9 number x letter or number.

i figure out how using - split, problem users use enter value out - , going strage if ask - in format. (in moment ask , pls enter format llll-999999-xxx wich no good.)

how can validate this, without using - , ideas? sorry im newbie @ javascript

<script language="javascript"> function rfc(cual) { mensaje = "pls enter format llll-999999-xxx" pat = /[a-z]|[a-z]/ pat2 = /[a-z]|[a-z]|[0-9]/ val = cual.split("-") if (val.length == 3){     if(val[0].length == 4){         if(!comp(val[0],pat)){             alert( mensaje)             return false             }         }     if(val[1].length == 6){         if(isnan(val[1])){             alert('no es un numero')             return false             }         }     if(val[2].length == 3){         if(!comp(val[2],pat2)){             alert(mensaje)             return false             }         } else{     alert(mensaje)     return false     } } else{     alert(mensaje)     return false     } return true } function comp(cual,pa){ for(m=0;m<cual.length;m++){     if(!pa.test(cual.charat(m))){         return false         break         }     } return true } </script> 

you use regex below:

/^[a-z]{4}[0-9]{6}[a-z0-9]{3}$/i 

if want - optional:

/^[a-z]{4}-?[0-9]{6}-?[a-z0-9]{3}$/i 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -