Jump to content

to validate special symbols


anushka

Recommended Posts

<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

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/93660-to-validate-special-symbols/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.