...

View Full Version : Required form fields



cyphix
06-26-2004, 10:22 PM
Say I have a form & I want to require people fill in form fields on the form & to have a alert box come up if they didn't fill one in (when they click the submit button) how can I do this?

Just say the form field names that are required are..

name
email


Cheers!

Willy Duitt
06-26-2004, 10:29 PM
Forum Search (http://www.codingforums.com/search.php?searchid=45905): keywords=required form fields.....

jamescover
06-27-2004, 10:12 PM
<script>
<!--
function checkForm(){
width=400;
height=300;
if (document.myForm.cbox.checked){
window.open('http://www.yourDomain.com/yourPage.html','yourWinName','width=400,height=300,top='+((screen.height-height)/2)+',left='+((screen.width-width)/2));
}else{
alert("You Must Agree To The Terms To Continue");
}
}
//-->
</script>




<form name="myForm" method="post" onsubmit="checkForm();return false;">
<textarea col="15" rows="2" wrap="virtual">Your Terms Go Here...</textarea>
<input type="checkbox" name="cbox" checked />
<input type="submit" name="myButton" value="Agree" />
</form>



-james

cyphix
06-27-2004, 10:27 PM
Did the search willy but couldn't really find anything suitable... seemed to just find people trying to fix their own code.

Thanks James.. but I need something that just checks to make sure they have filled in all requiref fields & if not pops an alert box, otherwise it continues with the submit action.

Garadon
06-27-2004, 11:10 PM
this only tested in IE but should work in Firefox to. basicly put the onclick event on the submit button and then the script in the head and write required in all the tags that are required.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script>
function tes(aForm){
var faults=new Array();
var fields=aForm.getElementsByTagName('input');
for(I=0;I<fields.length;I++){
if(fields[I].getAttribute('required'))
{
if(fields[I].value.replace(" ","")=="")
{
faults[faults.length]=fields[I].name+' field must be filled';
}
}
}
if(aForm.test.value.replace(" ","")=="")
{
faults[faults.length]='test field must be filled';
}
if(aForm.test1.value.replace(" ","")=="")
{
faults[faults.length]='test1 field must be filled';
}

if(faults.length>0){
alert(faults.join('\n\r'));
return false
}

return true;
}
</script>
</head>

<body>
<form>
<input type="text" name="test" required>
<input type="text" name="test1" required>
<input type="submit" onclick="return tes(this.form);" value="click">
</form>


</body>
</html>

cyphix
06-27-2004, 11:30 PM
Thanks for that... I'll test it out! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum