anushka Posted February 29, 2008 Share Posted February 29, 2008 <html> <head> <script language="text/JavaScript"> function ValidateForm() { var iChars = "!@#$%^&*()+=-[]';,./{}|":<>?"; for (var i = 0; i < document.form1.fname.value.length; i++) { if (iChars.indexOf(document.form1.fname.value.charAt(i)) != -1) { alert ("The box has special characters. \nThese are not allowed.\n"); return false; } return true; } </script> </head> <body> <form name="form1" id ="form1" action="1.php" method="POST" onsubmit='return ValidateForm();'> <input type="text" name="fname"/> <input name="submit" type="submit" value="Submit" onClick="ValidateForm();"/></td> </form> </body> </html> I am not able to validate the symbol Can any one help me whats the wrong with the code Quote Link to comment https://forums.phpfreaks.com/topic/93660-to-validate-special-symbols/ Share on other sites More sharing options...
Ads Posted March 1, 2008 Share Posted March 1, 2008 I would say it is Searching through The Form for the Whole string of Ichars. I am not that great With Java script So i don;t know for sure, but try Separating each Special Character with a Delimiter Quote Link to comment https://forums.phpfreaks.com/topic/93660-to-validate-special-symbols/#findComment-480607 Share on other sites More sharing options...
Psycho Posted March 2, 2008 Share Posted March 2, 2008 There are a few problems. 1. You are missing a closing "}". You have three opeinging curly brackets for the function, for the if statemetn and for the for loop, but there are only two closing curly brackets. 2. opening script tag is incorrect. It should be <script type="text/JavaScript"> 3. You have a double quote mark within the iChars variable, but the variable is enclosed in double quotes. So, you need to escape the one inside the quotes with a backslash: var iChars = "!@#$%^&*()+=-[]';,./{}|\":<>?"; Having said all that, however, this would be better done using a regular expression instead of looping through every character. Quote Link to comment https://forums.phpfreaks.com/topic/93660-to-validate-special-symbols/#findComment-481814 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.