Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to not allow special characters (Javascript) [closed]

I have a following requirement to only allow capital letters and , in a javascript form . I am unsure on how to check for special characters and script tags . I have written the following code . I do not want to allow characters such as $,%,& etc .

var upperCase= new RegExp('[A-Z]');
var lowerCase= new RegExp('^[a-z]');
var numbers = new RegExp('^[0-9]');

if($(this).val().match(upperCase) && $(this).val().match(lowerCase) &&   $(this).val().match(numbers))  
{
    $("#passwordErrorMsg").html("OK")

}
like image 874
user1801279 Avatar asked Nov 12 '13 15:11

user1801279


People also ask

How do I restrict characters in regex?

Go to the question's Settings. Go to Validation Criteria and choose the Manually enter your validation logic in XLSForm code option. In the Validation Code box, enter your regex formula between the quotation marks (' ') of the regex(., ' ') format. For reference, the period ( . )

How do you restrict special characters in a textbox using regular expression?

The simplest and fastest (performance-wise) way is to use the regular expression. ^[a-zA-Z0-9]*$ It matches any alphanumeric string (no spaces).


1 Answers

This may be helpful. javascript regexp remove all special characters if the only characters you want are numbers, letters, and ',' then you just need to whitespice all characters that are not those

$(this).val().replace(/[^\w\s\][^,]/gi, '')

like image 65
michael truong Avatar answered Oct 13 '22 15:10

michael truong