I want to check whether a string has any special characters or not. I am using this script:
var name = $("#name").val(); if(name.match(/[_\W]0-9/)) { alert('Not A Name'); } It doesn't alert even when name="sas23"
I want to check whether a string has any special characters or not. I am using this script:
var name = $("#name").val(); if(name.match(/[_\W]0-9/)) { alert('Not A Name'); } It doesn't alert even when name="sas23"
You should always take a whitelist approach when creating regular expressions. That means specify which characters are allowed, and ban everything else by default. If all you want is letters, then only allow letters:
var name=$("#name").val(); if(!name.match(/^[a-z]+$/i)) { alert('Not A Name'); }