Ok. How do I submit a form to a script that will validate it and if there are any errors, take them back and display a msgbox telling them that there are, and then submit the form to the script that adds it to the database?
Printable View
Ok. How do I submit a form to a script that will validate it and if there are any errors, take them back and display a msgbox telling them that there are, and then submit the form to the script that adds it to the database?
Make your submit buttons normal buttons and in their 'onClick' event handler, call the validation function. Put the Form.submit() at the end of the function and if one of the validations fail, alert the user and 'return' so it never gets to the submit. You could even set the focus to the offending field for them to fix.
I followed your instructions but I get error: Object doesn't support this property or method: 'frmTPOnline.submit'Quote:
Originally Posted by monte96
HTML Code:<!-- Data Validation-->
<SCRIPT LANGUAGE = "VBScript">
Sub Submit_OnClick()
if frmTPOnline.txtquantity.value = "" then
MsgBox "empty!"
exit Sub
else
msgbox "OK!"
end if
frmTPOnline.submit()
End Sub
</SCRIPT>
Its better to use JavaScript as VBScript is only supported in IE. Here is the standard way of validating a form.
HTML Code:<html>
<head>
<script>
function validate()
{
if (document.form1.txtName.value=="")
{
alert("Please enter your name");
document.form1.txtName.focus();
return false;
}
if (document.form1.txtEmail.value=="")
{
alert("Please enter your email");
document.form1.txtEmail.focus();
return false;
}
}
</script>
</head>
<body>
<form name=form1 onsubmit="return validate()">
Name : <input type=text name=txtName><br>
Email: <input type=text name=txtEmail>
<input type=submit>
</form>
</body>
</html>
Thank you for the code but in the intranet we use mostly VBscript.
I found the cause of the problem in my code: The form.submit couldnt be invoked because there was another object (button) with the name "submit" so there was conflict. I renamed the button to "cmdSubmit" and everything went fine since then.