0

quite an odd little problem that I am getting on this simple form field validation script.

I have 3 fields, 1 text input for name, 1 email input and 1 textarea for the comment.

Basically what happens now is that if you leave any field out and hit submit I assign either a .error or .valid class to the proper dom object.

The odd thing is however that if you miss out on one of the fields such as skip the name field, fill out the others, hit submit and then try to fill the name in, the validation even though it recognizes the field value as passing the regex (/.+/gi) it will apply the error class as it marks the field as failed.

Any ideas on what I am doing wrong would be much appreciated.

Code example is up on JS Fiddle: http://jsfiddle.net/jannis/YXfZN/

Note: This bug is happening and reproduced in the linked Fiddle in Chrome on Mac (v 12.0.742.112).

Thanks for taking a look, I look forward to your ideas and suggestions.

Jannis

1 Answer 1

1

That's because of g flag. Because of that your regex became sticky. Same problem with this: Regex in javascript fails every other time with identical input

If you test this code(regex[type].test( 'a' )) in console, you will get different result everytime.

Try again without g flag.

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

1 Comment

Awesome! Thanks so much for your comment. Didn't know that this was the critical bug :) Thanks also for the link, good info there.

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.