G
Gobind
Hi,
I am trying to submit a form and validate it (on client side using
javascript) before it gets committed to the database. What I do is
when the user hits the submit button, I follow to a JS function which
validates the data and returns a true if everything is ok, but a false
if something is wrong. In my mind, if the function returns a false,
the submit should not proceed. But for some reason, even after failing
validation, the form submits and tries to write this data to the
database. How can I stop this?
Just for reference, here is the code I am using:
//form definition
<form name="newpage" action="./task_submituser.jsp" method="post"
onSubmit="return validate_form()">
//submit button definition
<input type="submit" onclick="submit_click()" value="Submit Changes">
//function submit_click()
function submit_click()
{
document.newpage.newemail.value = document.newpage.email.value;
document.newpage.submit();
}
//function validate_form()
function validate_form()
{
var newname = document.newpage.username.value;
var newid = document.newpage.corpid.value;
var newphone = document.newpage.phone.value;
var newradio = document.newpage.radio.value;
var newemail = document.newpage.email.value;
if (document.newpage.dept_select.selectedIndex == 0)
{
alert("Invalid department.");
return false;
}
else
{
var newdept =
document.newpage.dept_select.options[document.newpage.dept_select.selectedIndex].value;
}
if ( isNaN( parseInt( newphone ) ) || newphone.length != 4 )
{
alert ("Your phone number must be 4 digits.");
return false;
}
if (newradio != "null" && newradio != 0 && newradio != "")
{
if ( isNaN( parseInt( newradio ) ) || newradio.length != 4)
{
alert ("Your radio number must be 4 digits.");
return false;
}
}
else
document.newpage.radio.value="0";
if ( newemail.search("@gm.com") != -1 || newemail.search("@") !=
-1 || newemail.search(".com") != -1 || newemail.search("--Enter") !=
-1 )
{
alert ("Invalid email address.");
return false;
}
return true;
}
Any help is greatly appreciated. Thanks in advance.
Kind regards,
Gobind Johar
I am trying to submit a form and validate it (on client side using
javascript) before it gets committed to the database. What I do is
when the user hits the submit button, I follow to a JS function which
validates the data and returns a true if everything is ok, but a false
if something is wrong. In my mind, if the function returns a false,
the submit should not proceed. But for some reason, even after failing
validation, the form submits and tries to write this data to the
database. How can I stop this?
Just for reference, here is the code I am using:
//form definition
<form name="newpage" action="./task_submituser.jsp" method="post"
onSubmit="return validate_form()">
//submit button definition
<input type="submit" onclick="submit_click()" value="Submit Changes">
//function submit_click()
function submit_click()
{
document.newpage.newemail.value = document.newpage.email.value;
document.newpage.submit();
}
//function validate_form()
function validate_form()
{
var newname = document.newpage.username.value;
var newid = document.newpage.corpid.value;
var newphone = document.newpage.phone.value;
var newradio = document.newpage.radio.value;
var newemail = document.newpage.email.value;
if (document.newpage.dept_select.selectedIndex == 0)
{
alert("Invalid department.");
return false;
}
else
{
var newdept =
document.newpage.dept_select.options[document.newpage.dept_select.selectedIndex].value;
}
if ( isNaN( parseInt( newphone ) ) || newphone.length != 4 )
{
alert ("Your phone number must be 4 digits.");
return false;
}
if (newradio != "null" && newradio != 0 && newradio != "")
{
if ( isNaN( parseInt( newradio ) ) || newradio.length != 4)
{
alert ("Your radio number must be 4 digits.");
return false;
}
}
else
document.newpage.radio.value="0";
if ( newemail.search("@gm.com") != -1 || newemail.search("@") !=
-1 || newemail.search(".com") != -1 || newemail.search("--Enter") !=
-1 )
{
alert ("Invalid email address.");
return false;
}
return true;
}
Any help is greatly appreciated. Thanks in advance.
Kind regards,
Gobind Johar