Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Validation

  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    160
    Thanks
    71
    Thanked 1 Time in 1 Post

    Validation

    Hey. Could someone point me in the direction of why this is not working, it worked in my last program, but not since I have changed things. I have a standard html form

    Code:
     <form method="POST" onsubmit="return validate_form(this)" action="anniversaryPreview.php">
        <p class="demo5">Senders Name<a class="textFields2"> 
        <input type="text" name="sender" value="e.g. Love from Nick" size="30" maxlength="35" /></a></p>
        <p class="btn2" >
        <input type="submit" value="Preview"></p>
        </form>

    And this should call up return validate_form(this) before bringing up the php page. Now the validator is just
    Code:
    function validate_required(field, alerttxt) {
            if (field.value == null || field.value == "") {
                    alert(alerttxt);
                    return false;
            }
            else {
                    return true;
            }
    }
    
    function validate_form(thisform) {
            if (validate_required(thisform.name,"Please specify the receivers name.")==false) {
                    thisform.name.focus();
                    return false;
            }
            if (validate_required(thisform.sender,"Please specify the sender's name.")==false) {
                    thisform.sender.focus();
                    return false;
            }
    }
    (This is not in the html file, but its own seperate file)
    I cant see if I have made any mistakes with the variables names or something, or what I am doing wrong. Dont get any errors, just nothing happens if I leave my forms fields blank. Anyone have an idea of whats going on here?

    cheers

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Ummm...
    Code:
    if (validate_required(thisform.name,"...
    Where in that <form>, pray tell, do you have any field
    Code:
        <input type="text" name="name" ...>
    ????
    The only text field I see there is the one named "sender".

    Oh, and you *ARE* getting errors. I would assume you are not using a debugger, so you simply aren't aware of them.




    I should note that your so-called validation is pretty useless. An entry of, say, a single space in the text field would be accepted by that code. Surely you should use more robust validation than that???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    160
    Thanks
    71
    Thanked 1 Time in 1 Post
    Ok, so changes have been made. I am doing this on a different form now. The form is
    Code:
    <form method="POST" onsubmit="return validateFormOnSubmit1(this)" action="sendEmail.php">
        <p class="demo2">Receivers E-mail<a class="textFields"> 
          <input type="text" name="receiver" size="30" maxlength="35" /></a></p>
        <p class="demo2">Senders E-mail<a class="textFields2"> 
          <input type="text" name="sender" size="30" maxlength="35" /></a></p>
        <p class="btn" ><input name="submit" type="submit" value="Send"/></p>
    </form>
    So that looks correct, with two fields sender and receiver. I then have another file, which holds the javascript code. This file is in the same folder as the above file. It looks like
    Code:
    function validateFormOnSubmit1(theForm) {
    var reason = "";
    
      reason += validateEmail1(theForm.receiver);
      reason += validateEmail1(theForm.sender);
            
      if (reason != "") {
        alert("Some fields need correction:\n" + reason);
        return false;
      }
    
      return true;
    }
    
    function trim(s)
    {
      return s.replace(/^\s+|\s+$/, '');
    }
    
    function validateEmail1(fld) {
        var error="";
        var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
        var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
       
        if (fld.value == "") {
            fld.style.background = 'Yellow';
            error = "You didn't enter an email address.\n";
        } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
            fld.style.background = 'Yellow';
            error = "Please enter a valid email address.\n";
        } else if (fld.value.match(illegalChars)) {
            fld.style.background = 'Yellow';
            error = "The email address contains illegal characters.\n";
        } else if (fld.value.length == 0) {
            fld.style.background = 'Yellow'; 
            error = "The required field has not been filled in.\n"
        } else {
            fld.style.background = 'White';
        }
        return error;
    }
    So, I am using receiver and sender, which are the variables from my form. However, it still goes straight through to the php file even if I enter an incorrect or empty email. I am using Dreamweaver, so dont know if this has a debugger. But, is there anything obviously wrong with what I am doing?

    cheers

  • #4
    Regular Coder
    Join Date
    May 2009
    Posts
    160
    Thanks
    71
    Thanked 1 Time in 1 Post
    Whoops, submit needs a capital S

    Actually, it still doesnt work for some reason.
    Last edited by nick2price; 06-15-2010 at 03:11 AM.


  •  

    Posting Permissions

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