469,277 Members | 2,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,277 developers. It's quick & easy.

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.php

Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_array($result))
  2.       echo "<td width='50' align='center'>$row[no]</td>";
  3.       echo "<td width='500' align='left'>$row[question]</td>";
  4.       echo "<td align='center' width='500'>               
  5.                 <input type='radio' name='rating[$i]' value='1'/>1                
  6.                 <input type='radio' name='rating[$i]' value='2'/>2
  7.                 <input type='radio' name='rating[$i]' value='3'/>3
  8.     <input type='radio' name='rating[$i]' value='4'/>4
  9.     <input type='radio' name='rating[$i]' value='5'/>5</td>";
  10.       echo "<td align='center' width='50'><input name='score[$i]' type='text' id='title'size='5' align='middle'></td>"
  11.      echo "<td><textarea name='remark[$i]' cols='25' id='title'></textarea></td>";
  12.      echo "</tr>";
  13.      $i++;
  14. }
  15.  
surveyForm.php

Expand|Select|Wrap|Line Numbers
  1. function validate()
  2. {     
  3. 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))
  4.      alert('Please choose the rating!'); 
  5.      return false 
  6. }
  7. else if (surveyForm.score.value == "")
  8. {
  9.      alert("Please insert mark.");
  10.      surveyForm.score.focus();
  11.      return false;
  12. }
  13.  
  14. else if(surveyForm.remark.value == "")
  15. {
  16.       alert("Please insert remark.");
  17.       surveyForm.remark.focus();
  18.       return false;
  19. }  
  20.    return true;
  21. }
  22.  
  23. <form name="surveyForm" id="surveyForm" method="post" action="surveyForm2.php" onSubmit="return validate()">
  24. <div align="center"><?php $survey->survey($form); ?></div>
  25. </form>
  26.  
below are the source code (radio button) for the document in the browser...

Expand|Select|Wrap|Line Numbers
  1. <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
  2. <input type='radio' name='rating[1]' value='2'/>2
  3. <input type='radio' name='rating[1]' value='3'/>3
  4. <input type='radio' name='rating[1]' value='4'/>4
  5. <input type='radio' name='rating[1]' value='5'/>5
  6.  
  7. <input type='radio' name='rating[2]' value='1'/>1
  8. <input type='radio' name='rating[2]' value='2'/>2
  9. <input type='radio' name='rating[2]' value='3'/>3
  10. <input type='radio' name='rating[2]' value='4'/>4
  11. <input type='radio' name='rating[2]' value='5'/>5
  12.  
  13. repeat until ...
  14.  
  15. <input type='radio' name='rating[7]' value='1'/>1
  16. <input type='radio' name='rating[7]' value='2'/>2
  17. <input type='radio' name='rating[7]' value='3'/>3
  18. <input type='radio' name='rating[7]' value='4'/>4
  19. <input type='radio' name='rating[7]' value='5'/>5
  20.  
for the score (textbox) and remark (textarea) also same like rating...array...any ideas???
Apr 21 '10 #1
13 1241
Dormilich
8,651 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. document.surveyForm.rating[1].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 getElementsByName() method. further, there is no single "rating[1]" form control, but 5 of it, so whose .checked property do you test?

untested
Expand|Select|Wrap|Line Numbers
  1. function groupIsChecked(l) 
  2. {
  3.     for (var i = l.length; i--;) {
  4.         if (true == l[i].checked) {
  5.             return true;
  6.         }
  7.     }
  8.     return false;
  9. }
  10. var list = document.getElementsByName("rating[1]");
  11. if (!groupIsChecked(list)) {
  12.     alert('Please choose the rating!');
  13.     return false;
  14. }
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.
Apr 21 '10 #2
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...

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language=JavaScript>
  2. function rdButton()
  3. {
  4.       var rdButtonSelected = false;
  5.       var selectedrdButton = document.getElementsByName("rating[1]"&&"rating[2]"&&"rating[3]"&&"rating[4]"&&"rating[5]"&&"rating[6]"&&"rating[7]");
  6.  
  7.       for(var i=0; i<selectedrdButton.length; i++)
  8.       {
  9.         if(selectedrdButton[i].checked == true)
  10.         {
  11.              rdButtonSelected = true;
  12.              break;
  13.         }
  14.       }
  15.  
  16.       if(rdButtonSelected == false)
  17.      {
  18.          window.alert("Please select the rating.");
  19.          return rdButtonSelected;
  20.      }
  21.  
  22.      else
  23.              return rdButtonSelected;
  24. }
  25.  
  26. function txtBox()
  27. {
  28.       var txtBoxSelected = false;
  29.       var selectedtxtBox = document.getElementsByName("score[1]"&&"score[2]"&&"score[3]"&&"score[4]"&&"score[5]"&&"score[6]"&&"score[7]");
  30.  
  31.       for(var i=0; i<selectedtxtBox.length; i++)
  32.       {
  33.          if(selectedtxtBox[i].checked == true)
  34.         {
  35.              txtBoxSelected = true;
  36.               break;
  37.         }
  38.       }
  39.  
  40.       if(txtBoxSelected == false)
  41.       {
  42.         window.alert("Please insert the marks.");
  43.         return txtBoxSelected;
  44.       }
  45.  
  46.       else
  47.              return txtBoxSelected;
  48. }
  49. </SCRIPT>
  50.  
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...

Expand|Select|Wrap|Line Numbers
  1. <form name="survey" id="survey" method="post" action="survey_code.php" onSubmit="return rdButton();txtBox();">
  2.  
Apr 24 '10 #3
Dormilich
8,651 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName("rating[1]"&&"rating[2]"&&"rating[3]"&&"rating[4]"&&"rating[5]"&&"rating[6]"&&"rating[7]");
that doesn’t work. getElementsByName() 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.
Apr 24 '10 #4
@Dormilich

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??
Apr 25 '10 #5
Dormilich
8,651 Expert Mod 8TB
is it because they are in array and looping,
you're not even getting there. you pass getElementsByName() a boolean ("rating[1]"&&"rating[2]" => true) which results in an empty NodeList.

you would have to loop over every rating separately.
Expand|Select|Wrap|Line Numbers
  1. var allRatings = ["rating[1]", "rating[2]", "rating[3]"];
  2. for (var i = allRatings.length; i--;) {
  3.     var rating = document.getElementsByName(allRatings[i]);
  4.     // validation comes here
  5. }
Apr 25 '10 #6
@Dormilich
Thanks for the reply.
Now, I used the code below to do the validation for textbox.

Expand|Select|Wrap|Line Numbers
  1. function txtBox()
  2. {        
  3.     var chks = document.getElementsByName("score[1]");
  4.  
  5.     for (var i = 0; i < chks.length; i++) 
  6.     {         
  7.            if (chks[i].value=="") 
  8.          { 
  9.                alert("Please insert the score"); 
  10.                chks[i].focus(); 
  11.                return false;             
  12.            } 
  13.     } 
  14. }
  15.  
Then I repeat as much as the textbox that I have with different score[] value for getElementsByName 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.
Apr 26 '10 #7
Dormilich
8,651 Expert Mod 8TB
I try to do it in different function, but the three functions did not work together.
something like
Expand|Select|Wrap|Line Numbers
  1. str = input.value;
  2. if (check1(str) && check2(str) && check3(str)) {
  3.     // do something if all true
  4. }
  5. // resp.
  6. if (!check1(str) && !check2(str) && !check3(str)) {
  7.     // do something if all false
  8. }
?
Apr 26 '10 #8
@Dormilich
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?
Apr 27 '10 #9
Dormilich
8,651 Expert Mod 8TB
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.
Apr 27 '10 #10
@Dormilich
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.
Expand|Select|Wrap|Line Numbers
  1. function validate()
  2. {
  3.     var numericExpression = /^[0-9.9]+$/;
  4.  
  5.     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))
  6.     {
  7.         alert("Please choose the rating.");
  8.         return false;
  9.     }
  10.  
  11.     else if(survey.title.value == "")
  12.     {
  13.         alert("Please insert score.");
  14.         survey.title.focus();
  15.         return false;
  16.     }
  17.  
  18.     else if(score.title.value.match(numericExpression))
  19.     {
  20.         return true;
  21.     }
  22.  
  23.     else
  24.     {
  25.         alert("Please insert the sscore in numeric.");
  26.         return false;
  27.     }
  28.  
  29.     return true;
  30. }
  31.  
Apr 27 '10 #11
Dormilich
8,651 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. function validate()
  2. {
  3.     var numericExpression = /^[0-9.9]+$/;
  4. // wrong syntax for your case
  5. // use: document.survey.["rating[1]"].checked, etc.
  6. // resp. if you have named a group: document.getElementsByName("rating[1]")[0].checked
  7.     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))
  8.     {
  9.         alert("Please choose the rating.");
  10.         return false;
  11.     }
  12. // where did you define survey?
  13. // do you mean document.survey.title.value ?
  14.     else if(survey.title.value == "")
  15.     {
  16.         alert("Please insert score.");
  17.         survey.title.focus();
  18.         return false;
  19.     }
  20.  // same as above
  21.     else if(score.title.value.match(numericExpression))
  22.     {
  23.         return true;
  24.     }
  25.  
  26.     else
  27.     {
  28.         alert("Please insert the sscore in numeric.");
  29.         return false;
  30.     }
  31.  
  32.     return true;
  33. }
Apr 27 '10 #12
@StarLavender
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.
Apr 29 '10 #13
Dormilich
8,651 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. if (/^\d+$/.test(document.surveyForm["score[0]"].value) === false) {
  2.     // correct input value
  3.     // show error message
  4. }
Apr 30 '10 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by Phil Powell | last post: by
4 posts views Thread by TG | last post: by
16 posts views Thread by Hosh | last post: by
9 posts views Thread by julie.siebel | last post: by
27 posts views Thread by Chris | last post: by
11 posts views Thread by Rik | last post: by
5 posts views Thread by lucyh3h | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.