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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Posts
    336
    Thanks
    43
    Thanked 2 Times in 2 Posts

    Suggestions for real-time contact form validation?

    Hello,

    I have an existing php contact form which incorporates php validation (required name with only characters, valid email address format, and minimum message length) and re-captcha.

    I would like to add some "real time" validation to the fields on my form (there are 3 fields - name, email and message) before the "Submit" button is pressed. For example when the user tabs from one text box to the next but hasn't filled in the required information.

    I am totally new to any type of java, but am I right in thinking I need either JavaScript, Ajax or JQuery to perform real-time validation?

    If so, which should I be looking into and are there any basic examples of form validation?

    I want to keep my pup validation as I'm aware that php is client-side but still needed as people can turn Java off in their web browsers.

    Any tips and pointers will be very much appreciated!

    Thankyou,

    Tom

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by moss2076 View Post
    Hello,

    I have an existing php contact form which incorporates php validation (required name with only characters, valid email address format, and minimum message length) and re-captcha.

    I would like to add some "real time" validation to the fields on my form (there are 3 fields - name, email and message) before the "Submit" button is pressed. For example when the user tabs from one text box to the next but hasn't filled in the required information.

    I am totally new to any type of java, but am I right in thinking I need either JavaScript, Ajax or JQuery to perform real-time validation?

    If so, which should I be looking into and are there any basic examples of form validation?

    I want to keep my pup validation as I'm aware that php is client-side but still needed as people can turn Java off in their web browsers.

    Any tips and pointers will be very much appreciated!

    Thankyou,

    Tom
    Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    Form validation is vanilla Javascript and has been covered a zillion times in this forum. Try using the search feature. A recent example I gave of email validation:-

    http://www.codingforums.com/showthread.php?t=245656

    Example of validation of a last name:-

    Code:
    var ln = document.myForm.lastName;
    var lnval = ln.value;
    if (lnval != "Last Name") {
    lnval = lnval.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    if((lnval.length <2) || (/[^a-z\-']/gi.test(lnval))) {  // hyphen and apostophe may occur in name Jones-O'Reilly
    alert ("Please enter your last name using only letters a-z, and hyphen/apostrophe if appropriate");
    document.myForm.lastName.value = "";
    myfield = ln;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);
    return false;
    }
    }
    As you seem to realise, JavaScript form validation only provides convenience for users, not security. If for example a script verifies that the user agreed to a firm's terms of service, or filters invalid characters out of fields that should only contain numbers, the validation must also be performed server-side, and not just on the client.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 12-08-2011 at 09:22 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    moss2076 (12-08-2011)

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Posts
    336
    Thanks
    43
    Thanked 2 Times in 2 Posts
    Thankyou for replying, I will check out the link you supplied

    Tom


  •  

    Posting Permissions

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