I use this in PHP to ensure a variable contains only alpha or numeric:
if(preg_match('/\W/', $username)) {
$loginerror[]=$siteLanguage->usernameinvalidchars;
};
How can I do the same check in JQUERY?
thx
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, you do not need jQuery for this – raw JavaScript is enough. More specifically:
test()method ofRegExpobject is enough (see documentation).Second, you can do this similarly to the following:
where
stris your string and/\W/.test(username)returnstrueif there is non-alphanumeric character withinusername(falseotherwise). See this jsfiddle for proof.