0

im trying to create a javascript classroom validator that checks if the user enters a valid classroom number.

Rules: Must be 4 digits Must be in the format of: 2 Capital Leters followed by 2 digits

What i have so far. this only checks for the length. im not sure how to go about doing the other validator.

function classRoom_validate(CLASS, max)
{
    var CLASS_len = CLASS.value.length;
    if (CLASS_len != max && CLASS.value.match()
{
    alert("Invalid classroom");
    CLASS.focus();
    return false;
}
return true;
}
1
  • Is this homework? If yes, add the homework tag. Commented Mar 20, 2012 at 20:47

2 Answers 2

3

You need a regular expression:

r = /[A-Z][A-Z]\d\d/

r.test('AA21')
true

r.test('blah')
false
Sign up to request clarification or add additional context in comments.

Comments

1

Use a Regex like this:

/[A-Z]{2}[0-9]{2}/.test(code);

/[A-Z]{2}[0-9]{2}/.test("AA12"); // true
/[A-Z]{2}[0-9]{2}/.test("Ab12"); // false
/[A-Z]{2}[0-9]{2}/.test("Abc2"); // false

etc

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.