Syed Ali said:
Hello,
I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.
If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.
If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.
How do I go about doing the above check?
You can't check that for sure, because you don't know that the visitor
has JavaScript enabled. If they have JavaScript enabled, you can check
it for their convenience, but you also have to be able to validate on
the server side, so you also need to provide a way for the server to
know which button was pressed.
This works on the client side, and also sends enough information to the
server to allow it to perform the same sort of validation.
Note that I'm taking your specification literally to mean that if the
second button is pressed, that *only* the first field should be filled:
<html>
<head>
<script type="text/javascript">
function validate(f,whic hCase){
var textfieldCount= 0;
var hasValueCount=0 ;
var firstHasValue=0 ;
for(var i=0;i<f.element s.length;i++){
if(f.elements[i].type=="text"){
textfieldCount+ +;
if(-1!=f.elements[i].value.search(/\S/)){
hasValueCount++ ;
if(textfieldCou nt==1){
firstHasValue++ ;
}
}
}
}
if(whichCase==" button1"){ // all must have values
if(hasValueCoun t==textfieldCou nt){
return true;
}else{
alert ("all fields must have values.");
return false;
}
}else{ // only first may have value
if((hasValueCou nt==1)&&(firstH asValue)){
return true;
}else{
alert("the first field (only) must have a value");
return false
}
}
}
</script>
</head>
<body>
<form onsubmit="retur n validate(this,w hichPressed)">
<input><input>< input><input>
<input name="whichSubm it" type="submit" value="button1"
onclick="whichP ressed=this.val ue">
<input name="whichSubm it" type="submit" value="button2"
onclick="whichP ressed=this.val ue">
</form>
</body>
</html>