jaymc Posted January 18, 2007 Share Posted January 18, 2007 I want to make it so on an input field on my form members can only type numbers and letters and no other charactersI was pretty sure their was a special html element that could achieve this but ive googled and havnt found anythingIf not, does anyone have a javascript that will do thisThanks! Quote Link to comment Share on other sites More sharing options...
nogray Posted January 18, 2007 Share Posted January 18, 2007 You can use regular expressions for that, here is a small sample[code]<script type="text/javascript">function cF(){ if (document.getElementById('input_field').value.search(/^[a-zA-Z0-9]+$/) == -1){ alert("invalid value"); return false; }}</script><form method="post" action="#" onSubmit="return cF()"><input type="text" name="input_field" id="input_field" /><input type="submit" value="Click" /></form>[/code] Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 18, 2007 Author Share Posted January 18, 2007 I was thinking of something that simply wouldnt allow them to type it in the first place rather than have alerts..Is their for sure no HTML alternitive? Quote Link to comment Share on other sites More sharing options...
nogray Posted January 18, 2007 Share Posted January 18, 2007 No, there is no HTML alternitive. If you don't want them to type it in the first place, you gotta add an onkeypress event and check the value each time the key is pressed. Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 19, 2007 Author Share Posted January 19, 2007 Cheers guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.