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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please Help with Form Validation

    Hi I have this code and it works well, but how do I add another check for valid.Surname ???

    Please Help


    // Validator Object
    var valid = new Object();

    // REGEX Elements

    // matches zip codes
    valid.PostCode = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$/;

    //matches email
    valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

    // matches phone ###-###-####
    //valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
    valid.ContactNo = /^0\d{2,4}[ -]{1}[\d]{3}[\d -]{1}[\d -]{1}[\d]{1,4}$/;


    function validateForm(theForm) {

    var elArr = theForm.elements;

    for(var i = 0; i < elArr.length; i++) {

    with(elArr[i]) {

    var v = elArr[i].validator;

    if(!v) continue;

    var thePat = valid[v];

    var gotIt = thePat.exec(value);

    if(! gotIt){
    alert(name + ": Please insert a valid " + v + " to " + value);
    elArr[i].select();
    elArr[i].focus();
    return false;
    }
    }
    }

    return true;

    }

  • #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
    valid.surName = /^([A-z]+)([\-\']?)([A-z]+)$/; // only letters, and optional hyphens (Smith-Jones) and apostrophe (O'Brian, B'stard) allowed, returns true if only these characters and no others are encountered in string.

    valid.surName = /([^A-z\-\'])/ // only letters, hyphens and apostrophe allowed, returns true if invalid characters encountered in string.

    A better email validator is

    valid.emailAddress = /^(\w)([a-z0-9_\.\-])+(\w)\@(([a-z0-9\-]?)+\.)+([a-z0-9]{2,4})$/i
    Last edited by Philip M; 05-09-2007 at 04:04 PM. Reason: add email regex


  •  

    Posting Permissions

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