Reputation: 79
Noob here wants to know how you can validate two fields using a javascript function. For example the online javascoipt code needs to validate two fields fieldx and fieldy
function checkdate(input) {
var validformat = /^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
var returnval = false
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.")
else { //Detailed check for valid date ranges
var monthfield = input.value.split("/")[0]
var dayfield = input.value.split("/")[1]
var yearfield = input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield - 1, dayfield)
if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
else
returnval = true
}
if (returnval == false) input.select()
return returnval
}
< /script>
Question How do I use the JavaScript to validated both the fields. I have tried && but didnt work.
<cfform action="someactionyoutake.cfm" method ="POST" onSubmit="return
checkdate(document.formname.fieldx && document.formname.fieldy)">
Thanks in advance
Upvotes: 0
Views: 1333
Reputation: 1008
Just change
<cfform action="someactionyoutake.cfm" method ="POST" onSubmit="return datecheck(document.formname.fieldx && document.formname.fieldy)">
to
<cfform action="someactionyoutake.cfm" method ="POST" onSubmit="return datecheck(document.formname.fieldx) && datecheck(document.formname.fieldy)">
Upvotes: 1
Reputation: 1392
As you have tagged the question with jquery try the following snippet
$(document).ready(function(){
var PreFormSubmitValidation = function (event) {
var isFormValid = true;
// your validation logic
if(<Validation Logic Fails>){
isFormValid = false;
}
if(!isFormValid){
return false;
// or can use
// event.preventDefault();
}
};
$("#formId").submit(PreFormSubmitValidation); // calls the method when the form is posted to the server
});
Hope it helps....
Upvotes: 0
Reputation: 655
just call an intermediate function :
<script>
function checkForm(){
return datecheck(document.formname.fieldx) && datecheck(document.formname.fieldy)
}
</script>
<cfform action="someactionyoutake.cfm" method ="POST" onSubmit="return checkForm()">
Upvotes: 1