User Id :    Password :      New Member   Forgot Password  
 
How to check if a string contains special charachter in javascript
Description How to check if a string contains special charachter in javascript   No. of Views     6501
  Rating     2
Author Najmul Hoda   Posted On     25 Aug 2011
Tags Java Script    

Sample Code   Download Code

 

Sometimes we need to check if a string contains any special charachter. Ex. name should not contains any special character.

Here is the codesnippet for the same.

 

function IsSpecialChar(strString)
//  check for valid SpecialChar strings
{
    var strValidChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}

 

Enjoy 

 

 

About Author

About Author He is a team lead, having 8+ years of experience. He loves writing technical article for .Net developers. Najmul Hoda
No Photo
 
Country United States
Company BirlaSoft
Home Page http://www.magixchat.com

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Annonymous on 02 Sep 2011 at 06:30 PM
 
Full of saielnt points. Don't stop believing or writing!
 
 
   
Write your comment here.
Comment
Verification Code