1

Ive been working on this regex for days now and I cant get it figured out. It either passes everything I put in there or it kicks everything out and I cannot seem to make it function. Admittedly I am new to doing this complex of stuff with Javascript so It may be that you realy cant do this.

I want to check onkeypress what was entered into the input and then validate it to x, y, or z. Then from there send it on about its way to do other neat stuff.

So the question is what the heck am I not understanding about RegExp?

Here is a FIDDLE for it.

    function val() {
    var gradeIn = document.querySelectorAll("#letGrade input[type=text]");
    var checkGrade = new RegExp(/[xyz]/gi);
    for (var i = 0; i < gradeIn.length; i++) {
        if (!checkGrade.test(gradeIn.value)) {
            alert ("This must be X, Y, or Z");
            return false;
        } else {
            return true;
        }
    }
};

EDIT/UPDATE: I was trying to do this on keypress and validate each text input individualy however this was realy kinda squishy in the grand scheme of things and not working out exactly correct. I decided to validate all text inputs onsubmit and have everything go all at once. Updated code is below.

function calcGPA() {
    var grades = document.querySelectorAll("#letGrade input[type=text]");
    var contacts = document.querySelectorAll("#conHours input[type=text]");
    var gVals = [];
    var cVals = [];
    var failGrade = "The Letter Grade input may only be A, B, C, D or F";
    var failHours = "The Contact Hours input may only be 1, 2, 3, 4 or 5";
    var checkGrade = /^[ABCDF]/;
    var checkhours = /^[12345]/;

    for (var i = 0; i < grades.length; i++) {
        if (!checkGrade.test(grades[i].value)) {
            alert(failGrade);
            return false;
        }
        if (!checkhours.test(contacts[i].value)) {
            alert(failHours);
            return false;
        }
        gVals.push(grades[i].value);
        cVals.push(contacts[i].value);
    }
    //Other cool stuff happens here
};

Now to just finish the conversion piece for the letters to numbers and the math piece. Thank you for your help on this!

1
  • regexp constructor takes a string and flags are separated by a comma Commented Nov 21, 2013 at 18:46

1 Answer 1

2

The problem's not only with your regular expression.

if (!checkGrade.test(gradeIn[i].value)) {

You weren't checking each grade. Now if you want it to only be those characters, you have to extend the regular expression a bit. Also, there's no point calling new RegExp if you're using native syntax.

var checkGrade = /^[xyz]+$/;

That means that you're OK with the fields being like "xxyyz" or "zzy". If it should just be one character, that'd be

var checkGrade = /^[xyz]$/;
Sign up to request clarification or add additional context in comments.

3 Comments

I commented earlier that that had looked like where the problem was but for some reason it still is not working. I dont know if Im doing something incorrect still or not. Im thinking the for loop is overkill? Why do I want to loop through all inputs when I am validating them individualy?
@OtisEugeneAnderson Well that's a good point. You really just need the validation routine to run against the input that's affected by the keypress.
Im going to accept your answer since it was the one that actually pointed me in the right direction. For some reason /^[xyz]/ returns the desired result but /^[xyz]$/does not. The $ sign I am assuming without looking to much into it was causing the check to stop after the first match was made and was ending the loop, hence the for was not completing entirely. However I took the $ out and now it works just fine. Thank for your help on this. Ill edit the op to show the newest version of the code.

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.