Javascript to check special characters


A quick code to check special characters in the user input in a form. Instead of increasing load on server by checking for this on Server side , this task can be easily performed on client side using javascript. The following function can be applied for this task

<script type=”text/javascript” language=“javascript”>

function check()
{
   var data=document.getElementById(“<%=txtCheck.ClientID %>”).value;
   var iChars = “!@#$%^&*()+=-[]’;,./{}”:?~_“;

   for (var i = 0; i if (iChars.indexOf(data.charAt(i)) != -1)
{
alert (“Your string has special characters. nThese are not allowed.“);
}
}

</script>

Finally call this function on OnClientClick event of the Button or any event of a control

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in Javascript and tagged . Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s