|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Dynamic form validation
I am using php and I want to validate the form with javascript. The validation is that they must enter something and it must be numeric. The problem is that the text boxes are created dynamically from mysql database.
here is my php code for the text boxes: PHP Code:
I want the error message to display in a pop up window stating something like you did not fill in all the fields and/or you did not enter numeric data. Thanks |
|
#2
|
||||
|
||||
|
possible answer
Hello!
you can try this: when you create your input, add the following code <input onfocusout="check(this);" name ="number_'.$i.$t.'" type="text" size="7"> and be sure to include this funciton on the page, this function can be like this: Code:
function check(obj)
{
if (obj == null)
return;
if (obj.value == "" || isNaN(obj.value))
alert("does not work");
}
this function chech for the data in teh "value property" of the passed object, if this data is null or is not a number, show an alert, of course you can change this for your own code, this is only an idea; thank you! |
|
#3
|
|||
|
|||
|
klur, good idea.
It may however cause a usability issue if someone gets multiple alert messages just from clicking in and out of the form fields (or using tab). I'd suggest dynamically writing javascript to perform your validation: 1. Add code for the onSubmit event of the form tag: <FORM NAME="myFrm" onSubmit="return validate_form(this);" ... > 2. Inside of your for loop (php code), add a line that will store all input fields as an array: Code:
<?
$fldArCount = 0;
for ($i=0; $i<$totalRows_Recordset1; $i++)
{
$row_Recordset1 = mysql_fetch_array($Recordset1);
echo "<tr>";
echo "<td>";
echo $row_Recordset1[Item_Name];
echo "</td>";
for ($t=0; $t <= 3; $t++)
{
$fldNameAr[$fldArCount] = 'number_'.$i.$t;
echo "<td>";
echo '<input name ="number_'.$i.$t.'" type="text" size="7">';
echo "</td>";
$fldArCount = $fldArCount + 1;
}
}
?>
</FORM>
<SCRIPT LANGUAGE="JAVASCRIPT">
// after the closing form tag, add the following:
function validate_form(fRef)
{
<?
for ($i = 0; $i < count($fldNameAr); $i++)
{
?>
if (fRef.<?=$fldNameAr[$i]?>.value == "")
{
alert("Please complete a value for <?=$fldNameAr[$i]?>");
fRef.<?=$fldNameAr[$i]?>.focus();
return false;
}
<?
}
?>
return true;
}
</SCRIPT>
hope this helps |
|
#4
|
||||
|
||||
|
Get around the multiple alerts issue by using onchange() rather than the nonstandard onfocusout() (which I had never heard of -- is it a variant of onblur()?). Masood, your code doesn't seem to validate number values (the whole point of validation in this case). Klur's code is cleaner and more readily extended (add a "type" parameter and then have your logic switch on types to do different checks and you've got the beginning of a full-featured validation script).
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
|
#5
|
|||
|
|||
|
Sorry, missed the numeric validation requirement ( that's what I get for typing in the middle of a conf call).
onchange would be a good solution to the alert windows. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Dynamic form validation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|