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
Share on other sites

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

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.