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

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Jannis
  • 17,025
  • 18
  • 62
  • 75

1 Answers1

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.

Community
  • 1
  • 1
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
  • Awesome! Thanks so much for your comment. Didn't know that this was the critical bug :) Thanks also for the link, good info there. – Jannis Jun 29 '11 at 19:28