0

I am validating a form and the text put into a form must be CIT, BIS or MATH

if(dept == ""){
     msg += "You must enter a department\n";
}else if((dept != "CIT") && (dept != "BIS") && (dept != "MATH")){
     msg += '"' + dept + '"' + " is not one of CIT, BIS, or MATH.\n";
}

I need to add some form of regex to make sure that the text CIT, BIS or MATH when input can be put in any form uppercase and lowercase.

Any help would be great Thanks

1
  • There is no need to use a regex here. Commented Nov 27, 2012 at 2:00

3 Answers 3

3

There's no need for a regex.

Just convert your input to lowercase before validating it.

if (dept == "") {
    msg += "You must enter a department\n";
}
else if (isInvalid(dept)) {
    msg += '"' + dept + '"' + " is not one of CIT, BIS, or MATH.\n";
}

function isInvalid(u) {
  var s = u.toLowerCase();
  var validStrings = [ "cit", "bis", "math" ];
  for (var i = 0; i < validStrings.length; i++) {
      if (s === validStrings[i]) {
          return false;
      }
  }
  return true;
}

As @y_nk has pointed out, the following approach would be more efficient (if necessary):

function isValid(s) {
    var validationObject = { "cit" : true, "bis": true, "math": true };
    return validationObject[ s.toLowerCase() ] === true;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Would be more straightforward with: function isValid(str) { return ({ "cit": true, "bis": true, "math": true })[str.toLowerCase()] === true; }
@y_nk - I don't see any benefit to that approach. I would rather write self-documenting code that is easy to read.
There's no for-loop, no need to loop two times if it matches the third value. Also the toLowerCase should belong to the validation function since it's the lowercasing is mandatory. For the inlining, i'm not very found of it... i only wrote it in a line because it's a comment.
@y_nk - After re-reading, I'll agree that your approach is technically more efficient. However, I would still opt for the more straightforward, easy to read approach (unless there was a real reason to modify it). Especially for an array with 3 elements and code that is clearly intended to be run as validation (where it probably will not be executed multiple times in a loop).
Its part of an assignment and i need to use regex at some point and there is the best point.
1

If you really think regular expressions are the way:

if (!/^(?:BIS|CIT|MATH)$/i.test(dept)) {
}

The expression had to be anchored, which is why you also see the non-capturing group in there.

A nicer approach is this:

if (['cit', 'bis', 'math'].indexOf(dept.toLowerCase()) == -1) {
}

Unfortunately, that only works from IE9 onwards due to Array.indexOf()

Comments

0

Something like this

var regex = new RegExp("^CIT$|^BIS$|^MATH$", "i");

if(dept == ""){
     msg += "You must enter a department\n";
}else if(!regex.test(dept)){
     msg += '"' + dept + '"' + " is not one of CIT, BIS, or MATH.\n";
}

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.