0
var pattern=/[A-za-z0-9]/;
if((fs.value||ls.value||ad1.value||ad2.value||pc.value||city.value||email.value)!=pattern)
{alert("fields not entered");
return false;}
else
return true;

Even after entering all the fields in the form, I get alert message "field not entered. Here fs,ls,ad1,ad2,email,pc and city are form field values

6
  • 1
    You are checking whether the last non-empty value is a regular expression, which will never be the case. Have a you looked at any documentation? developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Commented Apr 21, 2017 at 19:58
  • This is pretty easy to google ("javascript regexp"), one way is to use str.match(regexp) in your condition. Commented Apr 21, 2017 at 19:59
  • You can't just compare a string to a regex, you have to run it through a regex tester like ls.value.match(pattern) et al Commented Apr 21, 2017 at 20:00
  • Best site for learn regx regex101.com Commented Apr 21, 2017 at 20:02
  • Simple solution: var fields = [fs, ls, ...]; if (fields.every(f => pattern.test(f.value))) { ... }. Commented Apr 21, 2017 at 20:03

1 Answer 1

1

You need to take the values from those fields and run them thru the regex matching tool while passing the regex to that method.

var pattern=/[A-za-z0-9]/;
"string to check against regex pattern".match(pattern);

See more here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.