Hi
how I can use the java script in php to validate the form.
| SitePoint Sponsor |
Hi
how I can use the java script in php to validate the form.
Aneal
Use this (stolen) script:
<script language="JavaScript">
<!--
function MM_findObj(n, d) {
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function MM_validateForm() {
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') {
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>
(I don't undertand this btw, but it's very usable), then add a object to the submit button like this:
<input type=submit value=login onClick="MM_validateForm('name','','R','email','','RisEmail');return document.MM_returnValue">
This will check textfiel "name" for any characters and formfiel email for an email adres. I'm afraid that I have no documentation of this script. It's written by someone I don't know.
U can use R for text, Rismail for email, Risrange2:3 for a range of numbers.
Thank you for the script. But I can't use it, because first of all I can't understand it. and the next hing is I have many other fields, by using these codes I can't validate them.
Thanks again for your help.
Aneal



Here is some code for you to use:
Place that in your head of the HTML document.Code:<script Language="JavaScript"><!-- function form1(theForm) { // Validate that Text Fields And Text Areas have been filled in if (theForm.blah.value == "") { alert("Please enter a value for the \"Blah Blah\" field."); theForm.blah.focus(); return (false); } // Validate that a field is not longer than or smaller that if (theForm.zip.value.length > 10) { alert("Please enter at most 10 characters in the \"Zip Code\" field."); theForm.zip.focus(); return (false); } // Validate That Drop Down Menus Have been selected if (theForm.state.selectedIndex < 0) { alert("Please select one of the \"State\" options."); theForm.state.focus(); return (false); } // Validate that the first selection in a drop down menu has net bee selected if (theForm.state.selectedIndex == 0) { alert("The first \"State\" option is not a valid selection. Please choose one of the other options."); theForm.state.focus(); return (false); } return (true); } --> </script>
then this would be your form:
Just repeat the above stuff as needed for different fields and verification types. You should be ablke to figure out the code, real similiar to PHP.Code:<form action="confirm.php" method=post onsubmit="return form1(this)" name="form1"> <input type="text" name="blah"><br> <input type="text" name="zip"><br> <input type="select" size="1" name="state"> <option>Select A State</option> <option>WA</option> <option>ID</option> <option>OR</option> </select><br> <input type="submit" value="Submit" name="submit" onClick="form1"> </form>
God Bless,
Alex Stanton
Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.
Exbabylon- Professional Internet Services
This seems very usefull.. even better the the script I stole from some page![]()
Thnx
Thank you. This seems very useful. I got all the fields working but as soon a I entered the codes for e-mail it just stop working, I'm pasting my codes here for the e-mail validation let me know what I'm doing wrong here:
In this function I'm checking for the @ sign and the '.' :
function validEmail(email)
{
invalidChars = " /:,;"
if (email == "")
{
return false
)
for (i=0; i<invalidChars.length; i++)
{
badChar = invalidChars.charAt(i)
if (email.indexof(badChar,0) > -1)
{
return false
}
}
atPos = email.indexOf("@",1)
if (atPos == -1)
{
return false
}
if (email.indexOf("@", atPos + 1)
{
return false
}
periodPos = email.indexOf(".", atPos)
if (periodPos == -1)
{
return false
}
if (periodPos+3 > email.length)
{
return false
}
return true
}
And now I'm calling the e-mail validation function here:
function checkFields(confirm_form)
{
if (!validEmail(confirm_form.emailAddr.value))
{
alert(Invalid eamil address");
confirm_form.emailAddr.focus();
confirm_form.eamilAddr.select();
return false;
}
return true
}
Aneal
Never mind I got tht working, there was some syntax errors.
Thank you
Aneal
Bookmarks