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

    
    <script type="text/javascript">
    function checkcontact(){
    var str=document.myform.contact_name.value;
    var filter=/^([\w-]+(?:\.[\w-]+)*)((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if(filter.test(str))
    testresults=true;
    else {
    alert("Please input a valid contact name");
    return false;
    }
    </script>
    
    Code (markup):
    Also, I have this:
    <form action='form_handle.php' method='post' name='myform' onSubmit="return checkemail()">
    Code (markup):
    Should I add this:
    <form action='form_handle.php' method='post' name='myform' onSubmit="return checkcontact()">
    Code (markup):
    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:

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



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





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

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

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #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 Well-Known Member

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

                    <script type="text/javascript">
                        function checkemail(){
                            var str=document.myform.email_address.value;
                            var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
                            if (filter.test(str))
                                testresults=true;
                            else {
                                alert("Please input a valid email address!");
                                return false;
                            }
                            return true;
                        }
                        </script>
     
                    <script type="text/javascript">
                        function checkcontact(){
                        var str=document.myform.contact_name.value;
                        var filter=/^([\w-]+(?:\.[\w-]+)*)((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
                        if (filter.test(str))
                        testresults=true;
                        else {
                        alert("Please input a valid contact name");
                        return false;
                        }
                        return true;
                        }
                        </script>
     
            <script type="text/javascript">
            function checkform();
            {
            return checkcontact()&&checkemail();
            }
            </script>
    Code (markup):
    I also have this:
    <form action='form_handle.php' method='post' name='myform' onSubmit="return checkemail()">
    <form action='form_handle.php' method='post' name='myform' onSubmit="return checkform()">
    Code (markup):
    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 Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #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 Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #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,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #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 Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #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