SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help With Form Validation Needed

    This is in reference to the form here.

    In a form I am building the client wants the email field to be optional but still, when/if an email is typed in, the form still validates that it is a valid email. Also, the same is wanted for a cell phone number.

    I've tried to figure this out on my own and can't seem to do so. I have even gone to Google and nothing helpful comes up.

    Here is a link to the form I built: link

    Below is the javascript:

    Code:
    // form validation function //
    function validate(form) {
      var name = form.name.value;
      var hphonenumber = form.hphonenumber.value;
      var cphonenumber = form.cphonenumber.value;
      var email = form.email.value;
      var apptdate = form.apptdate.value;
      var appttime = form.appttime.value;
      var appttype = form.appttype.value;
      
      var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
      var phoneRegex = /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/;
      var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/; 
      var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
      
      if(name == "") {
        inlineMsg('name','You must enter your name.',2);
        return false;
      }
      if(!name.match(nameRegex)) {
        inlineMsg('name','You have entered an invalid name.',2);
        return false;
      }
      if(hphonenumber == "") {
        inlineMsg('hphonenumber','<strong>Error</strong><br />You must enter a phone number.',2);
        return false;
      }
      if(!hphonenumber.match(phoneRegex)) {
        inlineMsg('hphonenumber','You have entered an invalid phone number.',2);
        return false;
      }
      if(!cphonenumber.match(phoneRegex)) {
        inlineMsg('cphonenumber','You have entered an invalid phone number.',2);
        return false;
      }
      if(!email.match(emailRegex)) {
        inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
        return false;
      }
      if(apptdate == "") {
        inlineMsg('apptdate','<strong>Error</strong><br />You must enter an appointment date.',2);
        return false;
      }
      if(appttime == "") {
        inlineMsg('appttime','<strong>Error</strong><br />You must enter an appointment time.',2);
        return false;
      }
      if(appttype == "") {
        inlineMsg('appttype','You must enter a reason for your appointment request.');
        return false;
      }
      if(appttype.match(messageRegex)) {
        inlineMsg('appttype','You have entered an invalid message.');
        return false;
      }
      return true;
    }

  2. #2
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still need help.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi Matthew and welcome to Sitepoint

    I would suggest that a nested if() statement woudl do the trick.
    Code:
    if(email != "") {
    // if the email isn't blank
        if(!email.match(emailRegex)) {
        inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
        return false;
      }
    }
    something like that should work for both instances
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, I'll give that a try.

    Quote Originally Posted by spikeZ View Post
    Hi Matthew and welcome to Sitepoint

    I would suggest that a nested if() statement woudl do the trick.
    Code:
    if(email != "") {
    // if the email isn't blank
        if(!email.match(emailRegex)) {
        inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
        return false;
      }
    }
    something like that should work for both instances

  5. #5
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just implemented your suggestion and it works perfectly!

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    glad I could help
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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