www.webdeveloper.com
Results 1 to 3 of 3

Thread: little help with js cell validation

  1. #1
    Join Date
    Jun 2011
    Posts
    29

    little help with js cell validation

    i want to perform validation on the E-mail cell, when u dont type emails correctly to give the warning on the right side of the box "ERROR: not a valid e-mail address" or something..

    this is the html code

    Code:
    <form method="post" action="contact_process.php">
    <table>
    <tr>
    
                      <td><input name="email" onsubmit="return validateEmail()"  type="text" value="" size="32" maxlength="50"></td>
                    </tr>
    </table>
    </form>
    This is the js code..
    Code:
    // --------------------------------------------
    //               validateEmail
    // Validate if e-mail address
    // Returns true if so (and also if could not be executed because of old browser)
    // 
    
    function validateEmail (
    valfield, // element to be validated
    infofield, // id of element to receive info/error msg
    required) // true if required
    {
    var stat = commonCheck (valfield, infofield, required);
    if (stat != proceed) return stat;
    var tfld = trim(valfield.value); //whitespace trimmed off
    var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    if (!email.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid e-mail address");
    setfocus(valfield);
    return false;
    }
    var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
    if (!email2.test(tfld))
    msg (infofield, "warn", "Unusual e-mail address - check if correct");
    else
    msg (infofield, "warn", "OK");
    return true;
    }
    my problem is that this doesnt work.. i think the problem is on this specific part onsubmit="return validateEmail()"

    Edit: i want to mention that i want to "Live"-validate the email box, when u type the mail for example and u type telisgmail.com instead of telis@gmail.com or simply telis, the moment u press tab, or click somewhere out of the cell , if the email has not the correct form, the error msg must appear on the right. how do i do this?

    i think i must not use onsubmit to achieve this.. any help will be appreciated. thanks in advance.
    Last edited by telisjok; 06-23-2012 at 11:13 AM.

  2. #2
    Join Date
    Mar 2012
    Posts
    1,122
    Use ONBLUR.

  3. #3
    Join Date
    Jun 2011
    Posts
    29
    im driving crazy!! i cant solve this problem.. please any suggestions?

    i made these modifications to my code..

    HTML Code:
    <tr>
          <td><input type="text" name="email" maxlength="30" id="email" size="32" onChange="validateEmail(this, 'inf_email', true);" /></td><td id="inf_email"></td>
    </tr>
    js remained the same..

    Code:
    // --------------------------------------------
    //               validateEmail
    // Validate if e-mail address
    // Returns true if so (and also if could not be executed because of old browser)
    // 
    
    function validateEmail (
    valfield, // element to be validated
    infofield, // id of element to receive info/error msg
    required) // true if required
    {
    var stat = commonCheck (valfield, infofield, required);
    if (stat != proceed) return stat;
    var tfld = trim(valfield.value); //whitespace trimmed off
    var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    if (!email.test(tfld)) {
    msg (infofield, "error", "ERROR: not a valid e-mail address");
    setfocus(valfield);
    return false;
    }
    var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
    if (!email2.test(tfld))
    msg (infofield, "warn", "Unusual e-mail address - check if correct");
    else
    msg (infofield, "warn", "OK");
    return true;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles