It seems like no matter what input I give this, it always only matches 1 character. m.index
is always 0 on a match and m.length
is always 1. what am I doing wrong here? I have tried it at(and ripped off some code from) http://www.regular-expressions.info/javascriptexample.html and it works as expected there and matches an entire number.
You can see a live example here http://jsbin.com/aqobe
<html>
<head>
<script type="text/javascript">
function __numberBox__correctFormat(text,allow_float){
var r;
if(allow_float){
r=/\$?[\d,\.\W]+/;
}else{
r=/\$?[\d,\W]+/;
}
var m=r.exec(text);
if(m==null){
return false;
}
alert(m.index); alert(m.length);
if(m.index!=0 || m.length!=text.length){ //must match the whole string
return false;
}
return true;
}
</script>
</head>
<body>
Enter your name: <input type="text" id="fname" onchange="
if(__numberBox__correctFormat(this.value,true)){
alert('tis true');
}else{
alert('tis false');
}" />
</body>
</html>
Note I'm wanting for it to accept this input
1234 532,134 $123 493.29