Just a quick one from my head, haven't tried it...
First of all, set the maxlength=4, so they don't write anything else.
Second of all,
if(
isNaN(txtfield.charAt(0))||
isNaN(txtfield.charAt(1))||
txtfield.charAt(2) !=","||
isNaN(txtfield.charAt(3))
)
{
//error handling, format is not matching
}
else
{
//do what ever you want to do, format is matching
}
or something this way...?? My codes look like something a kid wrote I have absolutely no idea what I am talking about Somehow I still manage to make it work
here is an example:
~~~~~~~~
<html><head><title>validate to dd,d</title>
<script>
<!--
function validate(obj){ /*regular expression:
^ - begining of the string
\d - any digit (0..9)
{x} - repeat x times
\, - just a ,
*/
var re=/^\d{2}\,\d$/
var str=obj.value
if (!str) {alert("enter something"obj.focus(); return false} /*
if that expression would match the string,
then everything's good, we don't have to change anything
*/
if (!str.match(re)){ /* everythings baaaad, we have to get up, put our fingers
out of our nose & do something (should we?) */
//kicking out everything but digits
var tempstr=str.replace(/\D/g,'')
var len=tempstr.length
if (len<3){ //too short - lets add zeros
for (var ii=len; ii<=3; ii++) tempstr+="0"
}else{ //too long - would be truncated
tempstr=tempstr.substr(0,3)
} //making it look like you want:
tempstr=tempstr.replace(/(\d{2})(\d)/,'$1,$2')
obj.value=tempstr
}
}
//-->
</script>
</head>
<body>
<form name="form1">
<input type=text name=txt onchange="validate(this)"><br>
<input type=button onclick="validate(this.form.txt)" value="validate">
</form>
</body>
</html> ~~~~~~~~ Victor
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.