www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] Javascript Check Form - HELP

  1. #1
    Join Date
    Apr 2010
    Posts
    121

    resolved [RESOLVED] Javascript Check Form - HELP

    I currently have a form named "survey". I found here a JavaScript to validate my form. I apologize in advance if there is (and I know there must be) another post asking a similar question. and a JavaScript to set the form action and submit it when done here.

    I am having serious issues with this script, either it won't execute at all (the form doesn't work, as well as the validation) or the form submits without validating the form.

    Here is the current JavaScript I am using.

    In the head section
    Code:
    <script language="JavaScript"> 
    function checkform()
    {
    	if (!document.survey.look_feel[0].checked &&
    !document.feedback.look_feel[1].checked &&
    !document.feedback.look_feel[2].checked &&
    !document.feedback.look_feel[3].checked &&
    !document.feedback.look_feel[4].checked &&)
    	{
    		// something is wrong
    		alert('There is a problem with the first field');
    		return false;
    	}
    	else if (!document.survey.Registration[0].checked &&
    !document.feedback.look_Registration[1].checked &&
    !document.feedback.look_Registration[2].checked &&
    !document.feedback.look_Registration[3].checked &&
    !document.feedback.look_Registration[4].checked &&)
    	{
    		// something else is wrong
    		alert('There is a problem with...');
    		return false;
    	}
    	// If the script gets this far through all of your fields
    	// without problems, it's ok and you can submit the form
    
            else
      {
      document.survey.action = "http://www.fatcow.com/scripts/formemail.bml";
            document.survey.submit();
      }
    </script>
    in the body section

    Code:
    <form method="post" name="survey" id="survey" action="javascript:checkform()">
    Can anyone help?

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Remove && twice
    Code:
    [4].checked &&)
    Missing closing function bracket.
    Why are you using 2 forms?

    Don't rely on JavaScript:
    Code:
    <form method="post" name="survey" id="survey" action="javascript:checkform()">
    Better:
    Code:
    <form method="post" name="survey" id="survey" action="http://www.fatcow.com/scripts/formemail.bml" onsubmit="return checkform();">
    and replace the last else statement with return true;
    Last edited by Fang; 04-26-2010 at 03:05 AM.

  3. #3
    Join Date
    Apr 2010
    Posts
    121

    Exclamation Alright...

    Now i've got
    Code:
    <script language="JavaScript"> 
    function checkform()
    {
    	if (!document.survey.look_feel[0].checked &&
    !document.feedback.look_feel[1].checked &&
    !document.feedback.look_feel[2].checked &&
    !document.feedback.look_feel[3].checked &&
    !document.feedback.look_feel[4].checked)
    	{
    		// something is wrong
    		alert('There is a problem with the first field');
    		return false;
    	}
    	else if (!document.survey.Registration[0].checked &&
    !document.feedback.look_Registration[1].checked &&
    !document.feedback.look_Registration[2].checked &&
    !document.feedback.look_Registration[3].checked &&
    !document.feedback.look_Registration[4].checked)
    	{
    		// something else is wrong
    		alert('There is a problem with...');
    		return false;
    	}
    
    	// If the script gets this far through all of your fields
    	// without problems, it's ok and you can submit the form
    
            return true;
    }
    }	
    
    
    
    
    </script>
    and

    Code:
    <form method="post" name="survey" id="survey" action="http://www.fatcow.com/scripts/formemail.bml" onsubmit="return checkform();">
    It's still not working...

    I have an issue with brackets. Please Help!!!!

  4. #4
    Join Date
    Apr 2010
    Posts
    121

    Arrow

    Quote Originally Posted by SMTS View Post
    Now i've got
    Code:
    <script language="JavaScript"> 
    function checkform()
    {
    	if (!document.survey.look_feel[0].checked &&
    !document.feedback.look_feel[1].checked &&
    !document.feedback.look_feel[2].checked &&
    !document.feedback.look_feel[3].checked &&
    !document.feedback.look_feel[4].checked)
    	{
    		// something is wrong
    		alert('There is a problem with the first field');
    		return false;
    	}
    	else if (!document.survey.Registration[0].checked &&
    !document.feedback.look_Registration[1].checked &&
    !document.feedback.look_Registration[2].checked &&
    !document.feedback.look_Registration[3].checked &&
    !document.feedback.look_Registration[4].checked)
    	{
    		// something else is wrong
    		alert('There is a problem with...');
    		return false;
    	}
    
    	// If the script gets this far through all of your fields
    	// without problems, it's ok and you can submit the form
    
            return true;
    }
    }	
    
    
    
    
    </script>
    and

    Code:
    <form method="post" name="survey" id="survey" action="http://www.fatcow.com/scripts/formemail.bml" onsubmit="return checkform();">
    It's still not working...

    I have an issue with brackets. Please Help!!!!
    Note: The script is not working, but the form is still submiting. It does not check the feilds.

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Providing the html is correct it should work. What is the html?

  6. #6
    Join Date
    Apr 2010
    Posts
    121
    here is the source to "survey"

    View Source
    the coding is a little messy, mostely because of the "boxover" tooltip.

    Thankyou for your help thus far.

  7. #7
    Join Date
    Apr 2010
    Posts
    121

    resolved Figured it out...

    eager to release my survey, I got desperate. I quickly googled

    "Validate Form Scripts"

    Finding bunches of $700 software (that I didn't want) I changed my search to:

    "FREE Validate Form Scripts"

    Unfortunately, I came up with similar results. So... I searched for software. Remembering my first search results I typed:

    "FREE Validate Form Script Software"

    Nope... That didn't work either. Then I remembered when I first started learning HTML, when I wanted the image roll-over effect, I would go and find an HTML image roll-over generator. A thought strikes me...

    "Validation FREE Javascript Form", I type (in no specific order). I found what I needed. A fantastic service. Only one downside: You can only have 25 form fields. Luckily, I only had 12.

    So... for anyone who is looking for a solution, perhaps on google, and happened to stop by the forums:

    HERE IT IS
    Go to mindpalette

    Get the code... the instructions are there. Installing is easy. MINOR tweaks, in my case, were necessary - however in most cases it should not be.

    I know I did it the lazy way, however I have studied the script, and now I know how to check a form with JavaScript.

    For those coders out there, Here's what I got:

    Code:
    function validateForm() {
    with (document.survey) {
    var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
    radioOption = -1;
    for (counter=0; counter<look_feel.length; counter++) {
    if (look_feel[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nLook and Feel";
    radioOption = -1;
    for (counter=0; counter<Registration.length; counter++) {
    if (Registration[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nRegistration Process";
    radioOption = -1;
    for (counter=0; counter<Saftey.length; counter++) {
    if (Saftey[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nSafety of our Website";
    radioOption = -1;
    for (counter=0; counter<Useability.length; counter++) {
    if (Useability[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nUsability of our website";
    radioOption = -1;
    for (counter=0; counter<Uptime.length; counter++) {
    if (Uptime[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nServer Uptime";
    radioOption = -1;
    for (counter=0; counter<ucp.length; counter++) {
    if (ucp[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nUser Control Panel";
    radioOption = -1;
    for (counter=0; counter<news.length; counter++) {
    if (news[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nThe Newsletter";
    radioOption = -1;
    for (counter=0; counter<answer.length; counter++) {
    if (answer[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nThe answer to your question";
    radioOption = -1;
    for (counter=0; counter<time.length; counter++) {
    if (time[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nTime it took you to get an answer";
    if (Name.value == "") alertMsg += "\nYour Name";
    if (email.value == "") alertMsg += "\nYour E-mail";
    if (Username.value == "") alertMsg += "\nYour UserName";
    if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
    alert(alertMsg);
    return false;
    } else {
    return true;
    } } }
    And for those who want to see it in action... click here


    Good luck to all. I will try to mark this solved, close this topic, etc. But I don't think I have the permissions. Could someone do that for me?


    Thankyou Sooo Soooooo Much to these people:

    http://www.webdeveloper.com/
    internet.com
    and especially:
    "Fang"

    -----------

    Good day to you all.
    Last edited by SMTS; 04-27-2010 at 01:17 AM. Reason: Special Thanks

  8. #8
    Join Date
    Apr 2010
    Posts
    121

    Yah.

    The link is old. I have a new website, reinmcha.com, but I will not be putting this script on it anymore. This is as of 2011. SORRY.

    The solution still works though!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles