0

In my application is one of the fields I want to validate some special characters like `,"'/{}

If User-provided string in case character `,"'/{} matches I want to throw a validation message to the user. For that, I am trying to add validation to the input using regex. But I did get exact regex expression for selected special characters.

Please let me know anyone how to make our own regex or how to validate selected special characters.

function validateSpecialChars() {
var address = document.getElementById('address');
if (address.value == "") {
    alert("Address must be filled out");
    return false;
} else if (document.getElementById('address').value.length > 150) {
    alert("Address cannot be more than 150 characters");
    return false;
} else {
    var regex = /[-\/\\^$*+?.()|[\]{}]/;
    var isValid = regex.test(address);
    if (!isValid) {
        alert("Contains Special Characters.");
    } 
}
}
 Address: <input type="text" id="address"/>
    <br/>
    <br/>
    <input type="button" value="Submit" onclick="validateSpecialChars()"/>

0 Answers0