Form validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StarLavender
    New Member
    • Apr 2010
    • 33

    Form validation

    hi...anyone got ideas in form validation...I try a lot of methods already but still can't work...do not have any pop-up message although the required fill leave blank...is it the reason that I create my form in separate file??anyone got ideas on how to solve this problem?thanks in advance...

    part of the code

    class_survey.ph p

    Code:
    while($row = mysql_fetch_array($result))
    { 
          echo "<td width='50' align='center'>$row[no]</td>";
          echo "<td width='500' align='left'>$row[question]</td>";
          echo "<td align='center' width='500'>               
                    <input type='radio' name='rating[$i]' value='1'/>1                
                    <input type='radio' name='rating[$i]' value='2'/>2
                    <input type='radio' name='rating[$i]' value='3'/>3
    	<input type='radio' name='rating[$i]' value='4'/>4
    	<input type='radio' name='rating[$i]' value='5'/>5</td>";
          echo "<td align='center' width='50'><input name='score[$i]' type='text' id='title'size='5' align='middle'></td>"
         echo "<td><textarea name='remark[$i]' cols='25' id='title'></textarea></td>";
         echo "</tr>";
         $i++;
    }
    surveyForm.php

    Code:
    function validate()
    { 	
    if((document.surveyForm.rating[1].checked==false)&& document.surveyForm.rating[2].checked==false)&&(document.surveyForm.rating[3].checked==false))&&(document.surveyForm.rating[4].checked==false) &&(document.surveyForm.rating[5].checked==false&&(document.surveyForm.rating[6].checked==false)&&(document.surveyForm.rating[7].checked==false))
    { 
         alert('Please choose the rating!'); 
         return false 
    }
    else if (surveyForm.score.value == "")
    {
         alert("Please insert mark.");
         surveyForm.score.focus();
         return false;
    }
     
    else if(surveyForm.remark.value == "")
    {
          alert("Please insert remark.");
          surveyForm.remark.focus();
          return false;
    }  
       return true;
    }
    
    <form name="surveyForm" id="surveyForm" method="post" action="surveyForm2.php" onSubmit="return validate()">
    <div align="center"><?php $survey->survey($form); ?></div>
    </form>
    below are the source code (radio button) for the document in the browser...

    Code:
    <input type='radio' name='rating[1]' value='1'/>1<input type='radio' name='rating[1]' value='2'/>2<input type='radio' name='rating[1]' value='3'/>3<input type='radio' name='rating[1]' value='4'/>4<input type='radio' name='rating[1]' value='5'/>5 <input type='radio' name='rating[2]' value='1'/>1<input type='radio' name='rating[2]' value='2'/>2<input type='radio' name='rating[2]' value='3'/>3<input type='radio' name='rating[2]' value='4'/>4<input type='radio' name='rating[2]' value='5'/>5 repeat until ... <input type='radio' name='rating[7]' value='1'/>1<input type='radio' name='rating[7]' value='2'/>2<input type='radio' name='rating[7]' value='3'/>3<input type='radio' name='rating[7]' value='4'/>4<input type='radio' name='rating[7]' value='5'/>5<input type='radio' name='rating[1]' value='1'/>1
    <input type='radio' name='rating[1]' value='2'/>2
    <input type='radio' name='rating[1]' value='3'/>3
    <input type='radio' name='rating[1]' value='4'/>4
    <input type='radio' name='rating[1]' value='5'/>5
    
    <input type='radio' name='rating[2]' value='1'/>1
    <input type='radio' name='rating[2]' value='2'/>2
    <input type='radio' name='rating[2]' value='3'/>3
    <input type='radio' name='rating[2]' value='4'/>4
    <input type='radio' name='rating[2]' value='5'/>5
    
    repeat until ...
    
    <input type='radio' name='rating[7]' value='1'/>1
    <input type='radio' name='rating[7]' value='2'/>2
    <input type='radio' name='rating[7]' value='3'/>3
    <input type='radio' name='rating[7]' value='4'/>4
    <input type='radio' name='rating[7]' value='5'/>5
    for the score (textbox) and remark (textarea) also same like rating...array. ..any ideas???
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Code:
    document.surveyForm.[B]rating[1][/B].checked
    this is most probably the problem, since you don’t have a form control named "rating" (but "rating[1]"). you would have to use the getElementsByNa me() method. further, there is no single "rating[1]" form control, but 5 of it, so whose .checked property do you test?

    untested
    Code:
    function groupIsChecked(l) 
    {
        for (var i = l.length; i--;) {
            if (true == l[i].checked) {
                return true;
            }
        }
        return false;
    }
    var list = document.getElementsByName("rating[1]");
    if (!groupIsChecked(list)) {
        alert('Please choose the rating!');
        return false;
    }
    you could also set a default checked radio. once a group of radios is checked, there is no way without JavaScript to uncheck all of them.

    Comment

    • StarLavender
      New Member
      • Apr 2010
      • 33

      #3
      Thanks a lot...I change a bit with the code and it really work...I do the same to validate the textbox but it didn't work and it keep display the alert message only...

      Code:
      <SCRIPT language=JavaScript>
      function rdButton()
      {
            var rdButtonSelected = false;
            var selectedrdButton = document.getElementsByName("rating[1]"&&"rating[2]"&&"rating[3]"&&"rating[4]"&&"rating[5]"&&"rating[6]"&&"rating[7]");
      	
            for(var i=0; i<selectedrdButton.length; i++)
            {
      	    if(selectedrdButton[i].checked == true)
      	    {
                   rdButtonSelected = true;
      	         break;
      	    }
            }
      	
            if(rdButtonSelected == false)
           {
      	     window.alert("Please select the rating.");
      	     return rdButtonSelected;
           }
      	
           else
                   return rdButtonSelected;
      }
      
      function txtBox()
      {
            var txtBoxSelected = false;
            var selectedtxtBox = document.getElementsByName("score[1]"&&"score[2]"&&"score[3]"&&"score[4]"&&"score[5]"&&"score[6]"&&"score[7]");
      
            for(var i=0; i<selectedtxtBox.length; i++)
            {
      	     if(selectedtxtBox[i].checked == true)
      	    {
           	    txtBoxSelected = true;
      	          break;
      	    }
            }
      	
            if(txtBoxSelected == false)
            {
      	    window.alert("Please insert the marks.");
      	    return txtBoxSelected;
            }
      	
            else
                   return txtBoxSelected;
      }
      </SCRIPT>
      moreover, I try to do the validation for both of the function at the same time..but once the validation for radio button was completed, the function for validate the textbox didn't run...anyone got ideas on this??thanks in advance...

      the code I write to run both of the functions...

      Code:
      <form name="survey" id="survey" method="post" action="survey_code.php" onSubmit="return rdButton();txtBox();">

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Code:
        document.getElementsByName("rating[1]"&&"rating[2]"&&"rating[3]"&&"rating[4]"&&"rating[5]"&&"rating[6]"&&"rating[7]");
        that doesn’t work. getElementsByNa me() expects exactly on parameter and that is the string of the name (not a boolean).

        you would have to loop over all names explicitly (although you can package it in a loop)

        and text boxes don’t have a .checked property (That’s only for radios and checkboxes). you would have to test for the length (or validaty) of the .value property.

        Comment

        • StarLavender
          New Member
          • Apr 2010
          • 33

          #5
          Originally posted by Dormilich
          that doesn’t work. getElementsByNa me() expects exactly on parameter and that is the string of the name (not a boolean).

          you would have to loop over all names explicitly (although you can package it in a loop)

          and text boxes don’t have a .checked property (That’s only for radios and checkboxes). you would have to test for the length (or validaty) of the .value property.

          thanks...ya, I know the textbox need to validate by .value property but it does not work...why??is it because they are in array and looping, so they can't be validate as usual...then how??

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            is it because they are in array and looping,
            you're not even getting there. you pass getElementsByNa me() a boolean ("rating[1]"&&"rating[2]" => true) which results in an empty NodeList.

            you would have to loop over every rating separately.
            Code:
            var allRatings = ["rating[1]", "rating[2]", "rating[3]"];
            for (var i = allRatings.length; i--;) {
                var rating = document.getElementsByName(allRatings[i]);
                // validation comes here
            }

            Comment

            • StarLavender
              New Member
              • Apr 2010
              • 33

              #7
              Originally posted by Dormilich
              you're not even getting there. you pass getElementsByNa me() a boolean ("rating[1]"&&"rating[2]" => true) which results in an empty NodeList.

              you would have to loop over every rating separately.
              Code:
              var allRatings = ["rating[1]", "rating[2]", "rating[3]"];
              for (var i = allRatings.length; i--;) {
                  var rating = document.getElementsByName(allRatings[i]);
                  // validation comes here
              }
              Thanks for the reply.
              Now, I used the code below to do the validation for textbox.

              Code:
              function txtBox()
              {		
              	var chks = document.getElementsByName("score[1]");
                
                  for (var i = 0; i < chks.length; i++) 
                  {         
                     	if (chks[i].value=="") 
                   	{ 
                     		alert("Please insert the score"); 
                     		chks[i].focus(); 
                     		return false;             
                     	} 
                  } 
              }
              Then I repeat as much as the textbox that I have with different score[] value for getElementsByNa me and it work for me. But now, how am I going to validate the numeric field validation for the same texbox?I try to do it in different function, but the three functions did not work together. But when I just try two functions, the validation for radio button and textbox, they can work. May I know why they become like this and how to solve?Thanks in advance.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I try to do it in different function, but the three functions did not work together.
                something like
                Code:
                str = input.value;
                if (check1(str) && check2(str) && check3(str)) {
                    // do something if all true
                }
                // resp.
                if (!check1(str) && !check2(str) && !check3(str)) {
                    // do something if all false
                }
                ?

                Comment

                • StarLavender
                  New Member
                  • Apr 2010
                  • 33

                  #9
                  Originally posted by Dormilich
                  something like
                  Code:
                  str = input.value;
                  if (check1(str) && check2(str) && check3(str)) {
                      // do something if all true
                  }
                  // resp.
                  if (!check1(str) && !check2(str) && !check3(str)) {
                      // do something if all false
                  }
                  ?
                  Thanks for the reply. May I know that it is used for validation of empty textbox, right?or it can be used for both validation of check empty textbox and check the data must be numeric?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    that depends on what you put in the check functions. what the functions check for solely depends on your skill, programming validation code. you could even use one big function to check for check box, text box filled with correct data, etc.

                    Comment

                    • StarLavender
                      New Member
                      • Apr 2010
                      • 33

                      #11
                      Originally posted by Dormilich
                      that depends on what you put in the check functions. what the functions check for solely depends on your skill, programming validation code. you could even use one big function to check for check box, text box filled with correct data, etc.
                      ya. usually I will put all the validation in one big function, but this time they do not work. but when I separate them into different functions then some may work, but some not. I decide to put them in one big function again but still can't work.any problem exist?because I am not very familiar with javascript, so any comments are apreciated. Thanks.
                      Code:
                      function validate()
                      {
                      	var numericExpression = /^[0-9.9]+$/;
                      	
                      	if((!document.survey.rating[1].checked)&&(!document.survey.rating[2].checked)&&(!document.survey.rating[3].checked)&&(!document.survey.rating[4].checked)&&(!document.survey.rating[5].checked)&&(!document.survey.rating[6].checked)&&(!document.survey.rating[7].checked))
                      	{
                      		alert("Please choose the rating.");
                      		return false;
                      	}
                      	
                      	else if(survey.title.value == "")
                      	{
                      		alert("Please insert score.");
                      		survey.title.focus();
                      		return false;
                      	}
                      	
                      	else if(score.title.value.match(numericExpression))
                      	{
                      		return true;
                      	}
                      
                      	else
                      	{
                      		alert("Please insert the sscore in numeric.");
                      		return false;
                      	}
                      
                      	return true;
                      }

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Code:
                        function validate()
                        {
                            var numericExpression = /^[0-9.9]+$/;
                        // wrong syntax for your case
                        // use: [I]document.survey.["rating[1]"].checked[/I], etc.
                        // resp. if you have named a group: [I]document.getElementsByName("rating[1]")[0].checked[/I]
                            if((!document.survey.rating[1].checked)&&(!document.survey.rating[2].checked)&&(!document.survey.rating[3].checked)&&(!document.survey.rating[4].checked)&&(!document.survey.rating[5].checked)&&(!document.survey.rating[6].checked)&&(!document.survey.rating[7].checked))
                            {
                                alert("Please choose the rating.");
                                return false;
                            }
                        // where did you define survey?
                        // do you mean [I]document.survey.title.value[/I] ?
                            else if(survey.title.value == "")
                            {
                                alert("Please insert score.");
                                survey.title.focus();
                                return false;
                            }
                         // same as above
                            else if(score.title.value.match(numericExpression))
                            {
                                return true;
                            }
                         
                            else
                            {
                                alert("Please insert the sscore in numeric.");
                                return false;
                            }
                         
                            return true;
                        }
                        Last edited by Dormilich; Apr 27 '10, 01:41 PM. Reason: remembered the named groups

                        Comment

                        • StarLavender
                          New Member
                          • Apr 2010
                          • 33

                          #13
                          Originally posted by StarLavender
                          hi...anyone got ideas in form validation...I try a lot of methods already but still can't work...do not have any pop-up message although the required fill leave blank...is it the reason that I create my form in separate file??anyone got ideas on how to solve this problem?thanks in advance...

                          part of the code

                          class_survey.ph p

                          Code:
                          while($row = mysql_fetch_array($result))
                          { 
                                echo "<td width='50' align='center'>$row[no]</td>";
                                echo "<td width='500' align='left'>$row[question]</td>";
                                echo "<td align='center' width='500'>               
                                          <input type='radio' name='rating[$i]' value='1'/>1                
                                          <input type='radio' name='rating[$i]' value='2'/>2
                                          <input type='radio' name='rating[$i]' value='3'/>3
                          	<input type='radio' name='rating[$i]' value='4'/>4
                          	<input type='radio' name='rating[$i]' value='5'/>5</td>";
                                echo "<td align='center' width='50'><input name='score[$i]' type='text' id='title'size='5' align='middle'></td>"
                               echo "<td><textarea name='remark[$i]' cols='25' id='title'></textarea></td>";
                               echo "</tr>";
                               $i++;
                          }
                          surveyForm.php

                          Code:
                          function validate()
                          { 	
                          if((document.surveyForm.rating[1].checked==false)&& document.surveyForm.rating[2].checked==false)&&(document.surveyForm.rating[3].checked==false))&&(document.surveyForm.rating[4].checked==false) &&(document.surveyForm.rating[5].checked==false&&(document.surveyForm.rating[6].checked==false)&&(document.surveyForm.rating[7].checked==false))
                          { 
                               alert('Please choose the rating!'); 
                               return false 
                          }
                          else if (surveyForm.score.value == "")
                          {
                               alert("Please insert mark.");
                               surveyForm.score.focus();
                               return false;
                          }
                           
                          else if(surveyForm.remark.value == "")
                          {
                                alert("Please insert remark.");
                                surveyForm.remark.focus();
                                return false;
                          }  
                             return true;
                          }
                          
                          <form name="surveyForm" id="surveyForm" method="post" action="surveyForm2.php" onSubmit="return validate()">
                          <div align="center"><?php $survey->survey($form); ?></div>
                          </form>
                          below are the source code (radio button) for the document in the browser...

                          Code:
                          <input type='radio' name='rating[1]' value='1'/>1<input type='radio' name='rating[1]' value='2'/>2<input type='radio' name='rating[1]' value='3'/>3<input type='radio' name='rating[1]' value='4'/>4<input type='radio' name='rating[1]' value='5'/>5 <input type='radio' name='rating[2]' value='1'/>1<input type='radio' name='rating[2]' value='2'/>2<input type='radio' name='rating[2]' value='3'/>3<input type='radio' name='rating[2]' value='4'/>4<input type='radio' name='rating[2]' value='5'/>5 repeat until ... <input type='radio' name='rating[7]' value='1'/>1<input type='radio' name='rating[7]' value='2'/>2<input type='radio' name='rating[7]' value='3'/>3<input type='radio' name='rating[7]' value='4'/>4<input type='radio' name='rating[7]' value='5'/>5<input type='radio' name='rating[1]' value='1'/>1
                          <input type='radio' name='rating[1]' value='2'/>2
                          <input type='radio' name='rating[1]' value='3'/>3
                          <input type='radio' name='rating[1]' value='4'/>4
                          <input type='radio' name='rating[1]' value='5'/>5
                          
                          <input type='radio' name='rating[2]' value='1'/>1
                          <input type='radio' name='rating[2]' value='2'/>2
                          <input type='radio' name='rating[2]' value='3'/>3
                          <input type='radio' name='rating[2]' value='4'/>4
                          <input type='radio' name='rating[2]' value='5'/>5
                          
                          repeat until ...
                          
                          <input type='radio' name='rating[7]' value='1'/>1
                          <input type='radio' name='rating[7]' value='2'/>2
                          <input type='radio' name='rating[7]' value='3'/>3
                          <input type='radio' name='rating[7]' value='4'/>4
                          <input type='radio' name='rating[7]' value='5'/>5
                          for the score (textbox) and remark (textarea) also same like rating...array. ..any ideas???
                          is anyone know how to do the validation for the "score" textbox above?I want to validate so that only numeric can be accepted, character cannot.I already try a lot of methods but still can't work.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Code:
                            if (/^\d+$/.test(document.surveyForm["score[0]"].value) === false) {
                                // correct input value
                                // show error message
                            }

                            Comment

                            Working...