473,624 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form validation

33 New Member
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

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 1445
Dormilich
8,658 Recognized Expert Moderator Expert
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 getElementsByNa me() 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
StarLavender
33 New Member
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,658 Recognized Expert Moderator Expert
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. 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.
Apr 24 '10 #4
StarLavender
33 New Member
@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,658 Recognized Expert Moderator Expert
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.
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
StarLavender
33 New Member
@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 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.
Apr 26 '10 #7
Dormilich
8,658 Recognized Expert Moderator Expert
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
StarLavender
33 New Member
@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,658 Recognized Expert Moderator Expert
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

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

Similar topics

17
8281
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now with form elements that turn out to be arrays that have to be compared with one another! I have one form element, languages, a checkbox group. Beside each checkbox is a dropdown, proficiency (which will become proficiency alongside languages)....
4
9961
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when performing the validation check on the values that were entered into the form, I simply repost the form to perform the PHP validation. If any of the values that have been entered into the form are incorrect, I display a warning message on the screen...
4
2628
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a script that validates the form fields. the validation procedure is called ONCLICK event of the submit button. Follwowing is the structure of the validation procedure.
16
2222
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate individual fields, such as an "Email Validation Script" or a "Phone Validation Script". Is it ok to put all these scripts on page as they are or should they be joined in some way together to be one script? I'm a total JavaScript newbie and am completely...
9
4165
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be honest I usually hire someone to do it for me, grab predone scripts and kind of hack out the parts that I need, or just do very minimal validation (e.g. this is numeric, this is alpha-numeric, etc.)
27
4726
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
11
2986
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
10
5712
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the form the control is on? Basically I have a number of controls in a form that are required, and to check it I am setting the Validation Rule to "<>"IsNull" so that when the user tries to tab through/click out of a required area without entering...
5
3215
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The javascript method will do the form validation for each field one by one. For each field, an XMLHttpRequst will be made to a PHP file and get the return, either set an error field (<span>'s innerHTML) or leave it empty. Then I'll check the error field...
7
3599
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.