Forum Moderators: open

Message Too Old, No Replies

javascript input mask

         

paulanthony

8:59 am on Jun 14, 2006 (gmt 0)

10+ Year Member



I have a competition entry that must be in the format
3 characters then 4 numbers.

Anyone any ideas how to validate via javascript for this or to go about creating an input mask for this?

RonPK

11:10 am on Jun 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can match the entry against a regular expression pattern.

<script type="text/javascript"> 
function checkEntry(val) {
if (!val.match(/^[a-z]{3}[0-9]{4}$/i)) {
alert('no good.');
return false;
}
return true;
}
</script>
<form onsubmit="return checkEntry(this.entry.value)">
<input type="text" name="entry">
<input type="submit">
</form>

visionmonster

1:20 pm on Jun 14, 2006 (gmt 0)

10+ Year Member



glad RonPK did the heavy lifting on that one,

if you want the letters to be case insensitive you might need to tweak it a little from: [a-z] to [a-zA-Z]

some good reading
[regular-expressions.info...]

paulanthony

6:48 pm on Jun 14, 2006 (gmt 0)

10+ Year Member



Appreciate that chaps. Thanks.

RonPK

7:52 am on Jun 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you want the letters to be case insensitive

Hey, I did think of that ;) That is what the letter

i
after the pattern is for:
/^[a-z]{3}[0-9]{4}$/i