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! Link to comment https://forums.phpfreaks.com/topic/34738-input-restriction/ 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] Link to comment https://forums.phpfreaks.com/topic/34738-input-restriction/#findComment-163827 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? Link to comment https://forums.phpfreaks.com/topic/34738-input-restriction/#findComment-163992 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. Link to comment https://forums.phpfreaks.com/topic/34738-input-restriction/#findComment-164057 Share on other sites More sharing options...
jaymc Posted January 19, 2007 Author Share Posted January 19, 2007 Cheers guys Link to comment https://forums.phpfreaks.com/topic/34738-input-restriction/#findComment-164198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.