Check string contains special characters using JavaScript

 

Introduction: Hello Friends!! In this article I will explain about the implementation to check whether string contains special characters using JavaScript.

Implementation: The HTML page there will be two controls one is TextBox  for submitting input by the user. And other is button to validating or checking the string. Here we will assign JavaScript onclick event handler to the button.

Name:<input type="text" id="text_string" /> 

<input type="button" value="Submit" onclick="Validate)string()" />

 

When user will click the Button, the Validate JavaScript function will be call automatically. If entered string by the user i.e. TextBox value any character other than Alphabets, Numbers or Space then it will be as invalid and a error message will be displayed Error Message Box.

 <script type="text/javascript">

    function Validate_string() {

        //Regex for Valid Charactersi.e. Alphabets, Numbers and Space.

        var regex = /^[

 

        //Validate TextBox value against the Regex.

        var isValid = regex.test(document.getElementById("text_string").value);

        if (!isValid) {

            alert("There is Special Characters");

         }else {

            alert("There is no any Special Characters");

        }

 

        return isValid;

    }

</script>

Conclusion: In above code, I explained that how to check whether string contains special characters using JavaScript. This code is very helpful for every developer. Bye Bye everyone and take care of you Developers. We will come back shortly with the new article.

 

Regards

Using Asp.net

1 comment:

  1. Excellent article! The explanation of using JavaScript and regular expressions to detect special characters is clear, practical, and easy to follow. Input validation is an essential part of web development, and this example provides a simple yet effective approach that developers can quickly implement in their projects. Thank you for sharing such a useful and beginner-friendly tutorial that helps improve data quality and user input validation.
    lahore massage center
    shalwar kameez with waistcoat combination
    concrete recolour service

    ReplyDelete