0

I've got 3 fields that are used to enter 6 digits code for purpose of a database (Quickbase) query. Each field needs an individual alert/msg letting the submitter know if a code is invalid. Example:"The data entered in field #1 is invalid" . I put together the snippet below which somewhat achieves the results. Problem is when I enter just 1 field of bad data the script works but when I enter more than 1 field of bad data or mix good data with bad data the script does nto work properly... Thank you for your assitance.

Here are the fields

<input name="searchRecord" id="searchRecord1" type="text" size="8" maxlength="8" >
<input name="searchRecord" id="searchRecord2" type="text" size="8" maxlength="8">
<input name="searchRecord" id="searchRecord3" type="text" size="8" maxlength="8">
<input name="searchRecord" id="searchRecord4" type="text" size="8" maxlength="8"> 
<input name="searchRecord" id="searchRecord5" type="text" size="8" maxlength="8"> 
<input name="searchRecord" id="searchRecord6" type="text" size="8" maxlength="8">'

Here is the snippet

var js = req.responseText;
eval(js);

if (qdb_numrows == 0); {
    var x = true;
    var msg = "Precert 1:\n";
    if (document.getElementById('searchRecord1').value == qdb_numrows == 0) {
        if (qdb_numrows == 0) //only receive focus if its the first error
        document.getElementById('searchRecord1').focus();
        //change border to red on error (i would use a class change here...
        document.getElementById('searchRecord1').style.border = "solid 1px red";
        alert(msg = "Error: Data entered for field #1 not required for procedure code Or procedure code is not valid!\n");
        valid = false;
    }
    if (document.getElementById('searchRecord2').value == qdb_numrows == 0) {
        if (qdb_numrows == 0) document.getElementById('searchRecord2').focus();
        document.getElementById('searchRecord2').style.border = "solid 1px red";
        alert(msg += "Error: Data entered for field #2 not required for procedure code Or procedure code is not valid!\n");
        valid = false;
    }
    if (document.getElementById('searchRecord3').value == qdb_numrows == 0) {
        if (qdb_numrows == 0) //only receive focus if its the first error
        document.getElementById('searchRecord3').focus();
        //change border to red on error (i would use a class change here...
        document.getElementById('searchRecord3').style.border = "solid 1px red";
        alert(msg = "Error: Data entered for field #3 not required for procedure code Or procedure code is not valid!\n");
        valid = false;
    }
    if (document.getElementById('searchRecord4').value == qdb_numrows == 0) {
        if (qdb_numrows == 0) //only receive focus if its the first error
        document.getElementById('searchRecord4').focus();
        //change border to red on error (i would use a class change here...
        document.getElementById('searchRecord4').style.border = "solid 1px red";
        alert(msg = "Error: Data entered for field #4 not required for procedure code Or procedure code is not valid!\n");
        valid = false;
    }
    if (document.getElementById('searchRecord5').value == qdb_numrows == 0) {
        if (qdb_numrows == 0) //only receive focus if its the first error
        document.getElementById('searchRecord5').focus();
        //change border to red on error (i would use a class change here...
        document.getElementById('searchRecord5').style.border = "solid 1px red";
        alert(msg = "Error: Data entered for field #5 not required for procedure code Or procedure code is not valid!\n");
        valid = false;
    }
    if (document.getElementById('searchRecord6').value == qdb_numrows == 0) {
        if (qdb_numrows == 0) //only receive focus if its the first error
        document.getElementById('searchRecord6').focus();
        //change border to red on error (i would use a class change here...
        document.getElementById('searchRecord6').style.border = "solid 1px red";
        alert(msg = "Error: Data entered for field #6 not required for procedure code Or procedure code is not valid!\n");
        valid = false;
    }

    //if (!qdb_numrows==0) alert(msg);
    //return false;
}
Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
user1839308
  • 119
  • 1
  • 3
  • 13
  • 3
    Unrelated. Do you have to use `eval`? eval is evil. – beatgammit Nov 20 '12 at 19:37
  • 1
    I don't think this does what you think it does: `x == y == z` – beatgammit Nov 20 '12 at 19:38
  • In addition to @tjameson, you in one place say `msg += ...` and in all other places do `msg = ...`, you probably want to append. – Thomas Jones Nov 20 '12 at 19:52
  • No I don't use eval. I do think the error is in these lines ' if (document.getElementById('searchRecord6').value == qdb_numrows == 0)' I'm basically trying to tell it that if the value of in this case searchRecord6 is not in the db then post he alert – user1839308 Nov 20 '12 at 19:55
  • 1
    Holy hell man, why not use a validation library or at least write a reusable function? – Mike Robinson Nov 20 '12 at 19:56
  • 1
    @user1839308 your code snippet *is* using `eval`. And there is no triple equality check. Your evaluation will be akin to taking the result of `documnet.getElementById('searchRecord6').value == qbd_numrows` which is either `true` or `false` and then taking that value and comparing it against 0. So therefore your `if` statement will evaluate to true iff `documnet.getElementById('searchRecord6').value != qbd_numrows`, since `false == 0` is `true` in javascript thanks to loose typing. – Thomas Jones Nov 20 '12 at 20:05
  • Side note, use [triple equals](http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use) to avoid loose typing – Thomas Jones Nov 20 '12 at 20:05
  • Another issue, you have a semi-colon after your first `if` statement. Therefore all the code in your block will always get run, regardless if `qdb_numrows` is 0 or not. – Thomas Jones Nov 20 '12 at 20:06

0 Answers0