In this article I will explain with an example, how to check whether String contains Special Characters using JavaScript.
This article will illustrate how to use Regular Expression which allows Alphabets and Numbers (AlphaNumeric) characters with Space to filter out all Special Characters and verify whether String contains Special Characters.
HTML Markup
The following HTML Markup consists of an HTML TextBox and a Button.
Name: <input type="text" id="txtName"/>
<input type="button" value="Submit" onclick="Validate()"/>
Check whether String contains Special Characters using Regular Expression in JavaScript
When the Button is clicked, the Validate JavaScript function is called. Inside the Validate JavaScript function, the value of the TextBox is tested against the Regular Expression Alphabets and Numbers (AlphaNumeric) characters with Space.
Thus if the String i.e. the TextBox value consists of any character other than Alphabets, Numbers or Space then it will be considered invalid and the result will be displayed in JavaScript Alert Message Box.
<script type="text/javascript">
    function Validate() {
        //Regex for Valid Characters i.e. Alphabets, Numbers and Space.
        var regex = /^[A-Za-z0-9 ]+$/
        //Validate TextBox value against the Regex.
        var isValid = regex.test(document.getElementById("txtName").value);
        if (!isValid) {
            alert("Contains Special Characters.");
        } else {
            alert("Does not contain Special Characters.");
        return isValid;
Check whether String contains Special Characters using JavaScript
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.