Results 1 to 14 of 14
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    YAVQ (Yet Another Validation Question)

    I have 2 functions defined for checking the formatting of a date and a phone. these functions are called in a series of if statements in the final <font face="Georgia">validForm</font face=georgia>. I noticed that the functions only fire under certain conditions: the first test in the <font face="Georgia">validForm</font face=georgia> must fire for the second function to fire. to be specific, if the Date Completed field is filled in before the Work Phone field then the Work Phone validation function works. If you skip the date completed altogether the phone validation stuff dies. The wrapper for the functions looks like this:

    <font face="Georgia">function validateForm(theForm)
    {

    if (!validDate(theForm.Comp_Date_Completed, "Date Completed"))
    {
    return false;
    }

    if (!validPhone(theForm.Comp_Work_Phone, "Work Phone"))
    {
    return false;
    }
    ...

    }</font face=georgia>

    What seems to be happening is, if the first <font face="Georgia">if</font face=georgia> test doesn't get tickled, nothing else does. From what *I* can, the <font face="Georgia">validForm</font face=georgia> function is a series of if tests, so you'd think Javascript would step thru all of them before gleefully submitting bad data. This may be another case of me not understanding Javascript; if there's a better syntax or something obviously wrong here, please clue me in.

    I tried to add another function to test for a required field and when I did that I noticed this behaviour. <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: YAVQ (Yet Another Validation Question)

    Could you post a self-contained page for testing (e.g., in a zip archive)? It's impossible to follow the calls without seeing the full context.

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: YAVQ (Yet Another Validation Question)

    you asked for it, you got it!

    Since posting my question, I 'hard-coded' a test for the first date field, so you have to enter info into that field and then the other events fire. I commented out that code so you can experience (hopefully) what I've been looking at. I don't want to stick with the hard coded stuff as it is uncool. cool would be another function performing the same thing for as many things as I might want to require for a form.
    Attached Files Attached Files

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: YAVQ (Yet Another Validation Question)

    <P ID="edit" class=small>(Edited by jscher2000 on 31-Mar-04 14:10. Footnote)</P>This seems to run fine. It alerts, sets focus, and exits on the first problem it detects. I don't understand what isn't working in this version.

    > cool would be another function performing the same thing for as many things as I might want to require for a form

    Yeah, cool would be the script being able to figure out from some attribute of the field what kind of data it's supposed to contain and automatically running the correct validation routine, thereby letting you validate the entire field collection in one simple loop. But that might be a bit too much polymorphism to ask from JavaScript.

    FYI This phone string

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: YAVQ (Yet Another Validation Question)

    ok, try entering an invalid phone number ONLY. the script doesn't find anything wrong with the date field and sends the bad phone number along without a peep. if you enter something in the date field then the script will inspect the phone field and then go on to the next in line. that's what i meant by the validateForm not stepping thru each condition as I want it to.

    what I'm trying to implement is, tag each field with a name and run whichever function (validPhone, validDate, validZip, validSSN, requiredField) in a series of tests in the wrapper function validateForm(this). The problem is, like I said, Javascript seems to want the very first in the list to be triggered & if there is no problem there, the rest of the tests just don't fire. perhaps a Select Case syntax is needed? But why won't a series of separate if's work?...oh, I suppose if any test clears the validateForm(this) function passes return true; and that's all she wrote. but...but...WHY is the function stepping to the next test if and only if the first one is triggered? I mean, once you trigger and satisfy the first one, then the next in the queu comes up.

    and what's this <font face="Georgia">break;</font face=georgia> business? what does that do?

  6. #6
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: YAVQ (Yet Another Validation Question)

    Two options for you:
    1) Nested if statements
    if (condition) {
    if (condition) {
    if (condition) {
    return true;
    }
    }
    }
    else {
    return false;
    }

    2) Use a variable to determine if an item succeeds or fails, then return the result

    if (condition) {
    variable1 = true;
    }
    else {
    variable1 = false;
    }

    if (condition) {
    variable2 = true;
    }
    else {
    variable2 = false;
    }

    //etc...
    //finally
    if (variable1 && variable2) {
    return true;
    }
    else {
    return false;
    }

  7. #7
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: YAVQ (Yet Another Validation Question)

    <font color=red>still doesn't work! I still get same problem before, which is, if you don't put anything in the date field, the test for the phone field will not fire.</font color=red>

    arrgh!! <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

    thanks. I've been looking at something like
    <font face="Georgia">
    function validateForm(theForm){

    var i == 0

    If(condition1){
    i==1
    }

    if(condition2){
    1==1
    }

    ...

    if(i==0){
    return false;
    }
    ELSE{
    return true;
    }
    }
    </font face=georgia>
    I'll let ya'll know how that works.

    ALSO: what is the javascript equivilent of <font face="Georgia">instr</font face=georgia>

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: YAVQ (Yet Another Validation Question)

    Okay, I didn't test leaving the Date Completed field blank. When I do that I get a pop-up script error message that says: 'result' is undefined

    Check line 39, trace upwards, aha! your if clause does not create a value for result when the Date Completed field is blank. Add an else clause. Problem solved.

    You really need to turn on script error messages or you'll go mad. (And so will we. <img src=/S/smile.gif border=0 alt=smile width=15 height=15> )

    break, I believe, is used in a switch...case structure to skip the remaining actions in the structure. (VBA's approach seems more logical, limiting the case to its own specific actions, but this could be more efficient if the cases are cumulative.)

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: YAVQ (Yet Another Validation Question)

    For InStr() you use the indexOf() method.

    So...to illustrate:

    dim str1 as string
    str1 = "bagel"
    msgbox InStr(1, str1, "g") 'returns 3

    vs.

    var str1 = "bagel";
    alert(str1.indexOf("g", 0)); //returns 2

  10. #10
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: YAVQ (Yet Another Validation Question)

    thanks! I thought about an ELSE clause but I may not have used it correctly. although I have script debugging activated, it is of limited use. often if something is wrong with the code, the code doesn't seem to even process or I just get a line error attached to an error. sometimes it is helpful, of course, but it is a bit spartan. my guess is Javascript is designed to die easily so bad code won't stop pages from loading, but it does make debugging more difficult. do you know of a third-party de###### that is a bit more verbose? that'd also help in my forays in to scripting. (I should buy a book, too <img src=/S/smile.gif border=0 alt=smile width=15 height=15> )

    well, hopefully I can move on from here...

  11. #11
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: YAVQ (Yet Another Validation Question)

    thanks. i'm thinking of using this to test for invalid characters in the phone test as well as other validation functions.

  12. #12
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: YAVQ (Yet Another Validation Question)

    Steve, I hope this question isn't too direct. Have you considered using ASP.NET for this? The built-in validation components in ASP.NET alone are worth the switch! Plus, you can continue to use your existing server-side logic for form processing.

    I understand that ASP.NET may not be an option. After spending the past year writing exclusively .NET code, I must say that you won't regret it!

    Just my <img src=/S/2cents.gif border=0 alt=2cents width=15 height=15>

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: YAVQ (Yet Another Validation Question)

    Microsoft discontinued its InterDev product, or maybe incorporated it into something else. That was really its best shot at a script-development environment. Perhaps DreamWeaver is the next thing to evaluate?

    As for books, to answer your questions I've been using the O'Reilly book "Javascript, the definitive guide"

  14. #14
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: YAVQ (Yet Another Validation Question)

    i'll put in a request for this book and maybe someone has an edition available. in the meantime, I'll re-work my validation code with the ELSE statement to see if that gives me a better solution to the setup of the <font face="Georgia">validateForm(theForm)</font face=georgia> master function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •