0

I have a simple spam trap that asks users to type the color of something, currently the validation code is:

      if (theForm.color.value != "blue")
  {
    alert("Please Type The Color");
    theForm.color.focus();
    return (false);
  }

Unfortunately, this will return false if a user uses caps like Blue or BLUE. Is there a simple way to add acceptable values such as;

 if (theForm.color.value != "blue" "Blue") 

or

 if (theForm.color.value != "blue, Blue, BLUE")

Side Note: I'm unfamiliar with this language, what books would you suggest to get me started?

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • [JavaScript case insensitive string comparison](http://stackoverflow.com/questions/2140627/javascript-case-insensitive-string-comparison). You can use `.toLowerCase()` similarly. – Jonathan Lonowski Aug 21 '14 at 15:24

1 Answers1

0

Then you can simply convert it to lower case first and then check it. This way, you just need to add one condition and do the coding. JavaScript will convert the upper case letters to the lower case and then check the condition.

if (theForm.color.value.toLowerCase() != "blue") {
   // now compare it, it was hard to understand which language
   // you're using, JavaScript or C#
}

..other wise to add more cases you do this (More conditions)

if (theForm.color.value != "blue" && theForm.color.value != "Blue" && 
    theForm.color.value != "BLUE") {
    // code here..
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase

.toLowerCase() works on strings only.

Which language are you using by the way? Tell me, so that I can edit the answer if needed.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103