Javascript input Validation

THT

THT

Associate
Joined
10 Mar 2004
Posts
998
I need to validate a Text box using javascript

Id like to do it on keypress so that if a user enters a character they shouldnt, it displays a message.

The only characters that should be allowed are A-Z 0-9 and all punctuation
Im trying to do this to avoid people entering foreign characters such as Ĉ è æ

Should I do this using a regular expression?
Whats the easiest way to do this?
 
yeah you can use the onkeypress event

Its just I dont want to have to specify every character im allowing

A-Z 0-9 ,.><??'@;{[}]#~

Theres a lot of punctuation characters which I want to allow.... just not the foreign characters
 
You could use the onchange or one of the onkey events and a regular expression - however that all depends on your knowledge of regular expressions and if the expression is complex then you may struggle. Alternatively you can either list the accepted or rejected characters in a string and just perform an indexOf() using the last character in your string but as you say the list could become long.

Using a regular expression is probably your best bet, but Im struggling to understand what you are after? You seem to havea grasp of the concept of what yo uare doing, do you need help with anything in particular?
 
Thanks Rob,

I need to know if theres a way (or maybe the actual regexp?) for english chars only with punctuation.

Excluding things like Ĉ è æ ....
 
Back
Top Bottom