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 -
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 -
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... -
<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???
13 1439 Dormilich 8,658
Recognized Expert Moderator Expert - 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 - 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.
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... -
<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... -
<form name="survey" id="survey" method="post" action="survey_code.php" onSubmit="return rdButton();txtBox();">
-
Dormilich 8,658
Recognized Expert Moderator Expert - 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.
@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??
Dormilich 8,658
Recognized Expert Moderator Expert
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. - var allRatings = ["rating[1]", "rating[2]", "rating[3]"];
-
for (var i = allRatings.length; i--;) {
-
var rating = document.getElementsByName(allRatings[i]);
-
// validation comes here
-
}
@Dormilich
Thanks for the reply.
Now, I used the code below to do the validation for textbox. -
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 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.
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 - 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
-
}
?
@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?
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.
@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. -
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;
-
}
-
Dormilich 8,658
Recognized Expert Moderator Expert -
function validate()
-
{
-
var numericExpression = /^[0-9.9]+$/;
-
// wrong syntax for your case
-
// use: document.survey.["rating[1]"].checked, etc.
-
// resp. if you have named a group: document.getElementsByName("rating[1]")[0].checked
-
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 document.survey.title.value ?
-
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;
-
}
@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.
Dormilich 8,658
Recognized Expert Moderator Expert - if (/^\d+$/.test(document.surveyForm["score[0]"].value) === false) {
-
// correct input value
-
// show error message
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |