Hello I am doing form validations from javascript. I managed to do it but I want my code to be reusable for various forms. My idea is to put the name of the form as a parameter of the jS function but it is not working. Can anybody help me?
<form action="<?= FRONT_ROOT?>/user/edit" method="POST" name="editForm" onsubmit="return(validator());">
function validator() {
var emailID = document.editForm.email.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 )) {
alert("Please enter a valid email address.")
document.editForm.email.focus() ;
return false;
}
if( document.editForm.password.value.length < 8 ) {
alert( "The password must have at least 8 characters." );
document.editForm.password.focus() ;
return false;
}
return( true );
}
My idea is to do something like this but it doesn't work
<form action="<?= FRONT_ROOT?>/user/edit" method="POST" name="editForm" onsubmit="return(validator(this.name));">
function validator(genericForm) {
var emailID = document.genericForm.email.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 )) {
alert("Please enter a valid email address.")
document.genericForm.email.focus() ;
return false;
}
if( document.genericForm.password.value.length < 8 ) {
alert( "The password must have at least 8 characters." );
document.genericForm.password.focus() ;
return false;
}
return( true );
}