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 8 of 8

Thread: validation

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

    validation

    Hi, got my form here
    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>
    And that should call up this
    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;
    }
    For some reason though the validation is not working. Is there anything obvious I am doing wrong?
    cheers

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    It works for me. What exactly do you mean by "the validation is not working"?


    Quizmaster: In written communication, which country's name is an acronym for "I trust and love you"?
    Contestant: Germany.

  • Users who have thanked Philip M for this post:

    nick2price (06-17-2010)

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    160
    Thanks
    71
    Thanked 1 Time in 1 Post
    If i enter nothing into the form, and click enter, it goes through to the next page without giving me an error.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Sorry, I do not encounter that. Pressing the "Enter" key has no effect unless the validation routine has already been called, in which case it is called (properly) again.

    You have onsubmit="return validateFormOnSubmit1(this)" . That will work with any trigger for the submit. So if the form is completed then pressing the enter key will submit the form.
    Last edited by Philip M; 06-17-2010 at 05:07 PM.

  • #5
    Regular Coder
    Join Date
    May 2009
    Posts
    160
    Thanks
    71
    Thanked 1 Time in 1 Post
    Ok, this is what happens. I see the form on my webpage. I enter nothing at all. I click on the submit button. In doing this, I get taken to sendEmail.php. I thought that if i dont enter any data, I should get
    Code:
    if (fld.value == "") {
            fld.style.background = 'Yellow';
            error = "You didn't enter an email address.\n";
        }
    I dont get any of this.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You seem to be confused between the submit button (Send) and the enter key on the keyboard.

    But when I click "Send" with a blank unfilled form I get an alert:-
    Some fields need correction
    You did not enter an email address
    You did not enter an email address

    I see no reason why you should not get the same.

  • #7
    Regular Coder
    Join Date
    May 2009
    Posts
    160
    Thanks
    71
    Thanked 1 Time in 1 Post
    Ok, maybe it is working, but the way I might of done things may have caused a problem. Let me explain. That js code is in its own file. This file is in the same folder as the html form. All I do to the html form is add that part I have already shown you. For other things, I have things like
    Code:
    <script src="../../Scripts/swfobject_modified.js" type="text/javascript"></script>
    Do I need something like this for what I am using?

    cheers

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Maybe you can give us the URL of the page with the problem?

    Could be something else entirely.
    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.


  •  

    Posting Permissions

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