1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Adding this code to the form didn't work

Discussion in 'JavaScript' started by chrisj, Mar 15, 2013.

  1. #1
    I copied my Form code, which successfully makes entering an email into my form field mandatory, tweaked it slightly, hoping it would be enough to make adding a Contact Name mandatory, but it didn't work. What am I missing? You guidance will be appreciated. Thanks

    Code (Text):
    1.  
    2. <script type="text/javascript">
    3. function checkcontact(){
    4. var str=document.myform.contact_name.value;
    5. var filter=/^([\w-]+(?:\.[\w-]+)*)((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    6. if(filter.test(str))
    7. testresults=true;
    8. else {
    9. alert("Please input a valid contact name");
    10. return false;
    11. }
    12. </script>
    13.  
    Also, I have this:
    Code (Text):
    1. <form action='form_handle.php' method='post' name='myform' onSubmit="return checkemail()">
    Should I add this:
    Code (Text):
    1. <form action='form_handle.php' method='post' name='myform' onSubmit="return checkcontact()">
    What am I missing? Thanks for your guidance.
    chrisj, Mar 15, 2013 IP
  2. traianturcu

    traianturcu Member

    Messages:
    67
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #2
    So keep this part of you code:

    Code (Text):
    1. <script type="text/javascript">
    2. function checkcontact(){
    3. var str=document.myform.contact_name.value;
    4. var filter=/^([\w-]+(?:\.[\w-]+)*)((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    5. if(filter.test(str))
    6. testresults=true;
    7. else {
    8. alert("Please input a valid contact name");
    9. return false;
    10. }
    11. </script>



    You should create one more function:
    Code (Text):
    1. <script>
    2. function checkform()
    3. {
    4. return checkcontact()&&checkemail();
    5. }
    6. </script>
    The form should look like this:
    Code (Text):
    1. <form action='form_handle.php' method='post' name='myform' onSubmit="return checkform()">





    I also presume you also have a function called checkemail already defined.

    Hope this helps!
    Traian
    traianturcu, Mar 15, 2013 IP
  3. deathshadow

    deathshadow Prominent Member

    Messages:
    5,980
    Likes Received:
    827
    Best Answers:
    144
    Trophy Points:
    395
    #3
    can be done even simpler:

    onSubmit="return checkemail() && checkcontact();"

    Though really a good scripting check should be adding the submit check itself, NOT using the onsubmit attribute.

    NOT that I waste time checking this stuff client side since you still have to check it server side. If the resubmit of a populated form on error causes any sort of bandwidth, speed or usability issues compared to the WASTE of code added by scripted client side checks, there's probably something wrong with your form like unclear or lack of meaningful labels, unclear or lack of meaningful instructions, etc, etc.
    deathshadow, Mar 16, 2013 IP
  4. chrisj

    chrisj Active Member

    Messages:
    450
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Thanks for your replies. However what I have doesn't work for checking/requiring the contact name.

    Code (Text):
    1.                 <script type="text/javascript">
    2.                     function checkemail(){
    3.                         var str=document.myform.email_address.value;
    4.                         var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    5.                         if (filter.test(str))
    6.                             testresults=true;
    7.                         else {
    8.                             alert("Please input a valid email address!");
    9.                             return false;
    10.                         }
    11.                         return true;
    12.                     }
    13.                     </script>
    14.  
    15.                 <script type="text/javascript">
    16.                     function checkcontact(){
    17.                     var str=document.myform.contact_name.value;
    18.                     var filter=/^([\w-]+(?:\.[\w-]+)*)((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    19.                     if (filter.test(str))
    20.                     testresults=true;
    21.                     else {
    22.                     alert("Please input a valid contact name");
    23.                     return false;
    24.                     }
    25.                     return true;
    26.                     }
    27.                     </script>
    28.  
    29.         <script type="text/javascript">
    30.         function checkform();
    31.         {
    32.         return checkcontact()&&checkemail();
    33.         }
    34.         </script>
    I also have this:
    Code (Text):
    1. <form action='form_handle.php' method='post' name='myform' onSubmit="return checkemail()">
    2. <form action='form_handle.php' method='post' name='myform' onSubmit="return checkform()">
    If you could tell me what I have wrong, and what is keeping this from requiring filling in the contact name field, I'd greatly appreciate it.
    chrisj, Mar 16, 2013 IP
  5. deathshadow

    deathshadow Prominent Member

    Messages:
    5,980
    Likes Received:
    827
    Best Answers:
    144
    Trophy Points:
    395
    #5
    Why do you have two form tags? You should only have ONE form tag. You are also using three script tags where you should only be using one... It's also outdated methodology to use the names of your elements to access the tag you are validating, particularly since they may not be unique on the page... not sure why you are storing the 'results' in 'testresults' since you don't seem to need or use it, AND it would conflict between the routines...

    Also the contact name check seems needlessly convoluted -- it's not an e-mail, so long as it's not empty, why go nutters on it?

    Could we see your entire form? I've got some handler scripts I used to use for doing this I could reasonably quickly apply if I could see the entire thing instead of just the FORM tag. You're trying to manually check form elements by targeting them instead of parsing them off the DOM -- which would really be the saner method of doing this.
    deathshadow, Mar 16, 2013 IP
  6. chrisj

    chrisj Active Member

    Messages:
    450
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Thank you for your reply. Greatly appreciated. I know the Form has some issues, but I'd simply prefer to fix what I've got than to get another Form. So, I'd greatly appreciate some help just fixing this one, please.
    Thanks again.
    chrisj, Mar 16, 2013 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,883
    Likes Received:
    33
    Best Answers:
    51
    Trophy Points:
    100
    #7
    Instead of getting another form (whatever that means), how about just writing one that's correct? A contact form is one of the most trivial things you can write.
    Rukbat, Mar 17, 2013 IP
  8. chrisj

    chrisj Active Member

    Messages:
    450
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #8
    Thanks for your replies.
    My Form works successfully with just the email being a mandatory field.
    I simply want help to add making the Contact Name field mandatory.
    chrisj, Mar 18, 2013 IP