Noob在这里想知道如何使用javascript函数验证两个字段。 例如,在线javascoipt代码需要验证两个字段fieldx和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>
问题 如何使用JavaScript验证这两个字段。我试过&amp;&amp;但没有工作。
<cfform action="someactionyoutake.cfm" method ="POST" onSubmit="return
checkdate(document.formname.fieldx && document.formname.fieldy)">
提前致谢
答案 0 :(得分:1)
只需调用一个中间函数:
<script>
function checkForm(){
return datecheck(document.formname.fieldx) && datecheck(document.formname.fieldy)
}
</script>
<cfform action="someactionyoutake.cfm" method ="POST" onSubmit="return checkForm()">
答案 1 :(得分:1)
只需更改
<cfform action="someactionyoutake.cfm" method ="POST" onSubmit="return datecheck(document.formname.fieldx && document.formname.fieldy)">
到
<cfform action="someactionyoutake.cfm" method ="POST" onSubmit="return datecheck(document.formname.fieldx) && datecheck(document.formname.fieldy)">
答案 2 :(得分:0)
如果您使用 jquery 标记了问题,请尝试以下代码段
$(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
});
希望它有所帮助......