learn
asked on
check a form
How to check the validity of a form in a page when the user click the submit button?
I have done a little bit a few years ago, but can not remember anymore ;-( Something like: (?)
<FORM method=post action="mailto:myEmailAddr ess"
onsubmit="return checkform(this)">
(If need script, tell me the simple VBScript code)
I have done a little bit a few years ago, but can not remember anymore ;-( Something like: (?)
<FORM method=post action="mailto:myEmailAddr
onsubmit="return checkform(this)">
(If need script, tell me the simple VBScript code)
<script....>
sub validate()
if .... then 'if u feel the input is not ok
msgbox("error, pls input again")
return
else
document.myform.submit()
end sub
.....
</script>
.....
<form name=myform method=post action="mailto:myEmailAddr ess" >
....
<a href="validate()">submit here!</a>
....
</form>
the link "submit here" lead u to the validate function in script, if the input r ok, it will submit the form, or it will warn with a message box
try it and good luck
:)
sub validate()
if .... then 'if u feel the input is not ok
msgbox("error, pls input again")
return
else
document.myform.submit()
end sub
.....
</script>
.....
<form name=myform method=post action="mailto:myEmailAddr
....
<a href="validate()">submit here!</a>
....
</form>
the link "submit here" lead u to the validate function in script, if the input r ok, it will submit the form, or it will warn with a message box
try it and good luck
:)
ASKER
To freshmeat:
Thank you.
1. I have just tried your code without success :-(
Error was: Netscape is unable to find the file or directory named /CI/.../validate().
2. And can we put the "return" in the tag like what in my question?
Thank you.
1. I have just tried your code without success :-(
Error was: Netscape is unable to find the file or directory named /CI/.../validate().
2. And can we put the "return" in the tag like what in my question?
ASKER
To oubelkas:
Thank you.
I have managed to code that in JavaScript! Can you check that for me and explain the use of "return" in the tag:
<html><head><script>
function test()
{alert ("alerting");
return false;}
</script></head>
<body>
<FORM method=post action="mailto:myEmailArre ss">
<INPUT type=submit value="Send" onclick="return test()">
</FORM>
<body></html>
Thank you.
I have managed to code that in JavaScript! Can you check that for me and explain the use of "return" in the tag:
<html><head><script>
function test()
{alert ("alerting");
return false;}
</script></head>
<body>
<FORM method=post action="mailto:myEmailArre
<INPUT type=submit value="Send" onclick="return test()">
</FORM>
<body></html>
Well, in this case you can leave the return statement. Most of the time it's used to pass the values which are defined in the FORM. I already said I had a javascript that checked a form.
You can still have it if you want? I must give it to you tomorrow because now I have to go...
Joseph
You can still have it if you want? I must give it to you tomorrow because now I have to go...
Joseph
learn,
I don't know if you have ASP or ColdFusion available to you, but error checking on a form is pretty easy using those two languages.
bruno
I don't know if you have ASP or ColdFusion available to you, but error checking on a form is pretty easy using those two languages.
bruno
Check this out :
<html>
<head>
<title> check form </title>
<script language="javascript">
<!--
function checkAllValues(myForm){
if (myForm.mySelect.options[0 ].selected == true ) {
alert ('Select something!');
myForm.mySelect.focus();
return (false);
}
if (myForm.myInput.value == ""){
alert ('Put something in the textarea!');
myForm.myInput.focus();
return (false);
}
return (true);
}
// -->
</script>
</head>
<body>
<table border="0">
<form name="myForm" method="get" onSubmit="return checkAllValues(this)" >
<tr>
<td valign="top" align="right"><b>Select something </b></td>
<td valign="top" align="left"><select name="mySelect">
<option value="">
<option value="one"> one...
<option value="two"> or pick two
<option value="three"> or maybe three
</select></td>
</tr>
<tr>
<td valign="top" align="right"><b>Put something here!</b></td>
<td valign="top" align="left"><textarea name="myInput" rows="6" cols="55"></textarea></td>
</tr>
<tr>
<td></td><td>
<input type="submit" name="mySubmit" value="check it">
<input type="reset" name="myReset" value="Reset">
</td>
</tr>
</form>
</table>
</body>
</html>
Here you can see in the onSubmit event handler of the form, the keyword "return" has been used. This returns the values given in the form. The values are defined with "this" in the function.
Joseph
<html>
<head>
<title> check form </title>
<script language="javascript">
<!--
function checkAllValues(myForm){
if (myForm.mySelect.options[0
alert ('Select something!');
myForm.mySelect.focus();
return (false);
}
if (myForm.myInput.value == ""){
alert ('Put something in the textarea!');
myForm.myInput.focus();
return (false);
}
return (true);
}
// -->
</script>
</head>
<body>
<table border="0">
<form name="myForm" method="get" onSubmit="return checkAllValues(this)" >
<tr>
<td valign="top" align="right"><b>Select something </b></td>
<td valign="top" align="left"><select name="mySelect">
<option value="">
<option value="one"> one...
<option value="two"> or pick two
<option value="three"> or maybe three
</select></td>
</tr>
<tr>
<td valign="top" align="right"><b>Put something here!</b></td>
<td valign="top" align="left"><textarea name="myInput" rows="6" cols="55"></textarea></td>
</tr>
<tr>
<td></td><td>
<input type="submit" name="mySubmit" value="check it">
<input type="reset" name="myReset" value="Reset">
</td>
</tr>
</form>
</table>
</body>
</html>
Here you can see in the onSubmit event handler of the form, the keyword "return" has been used. This returns the values given in the form. The values are defined with "this" in the function.
Joseph
ASKER
To Joseph:
That is the code I am interested!
About "return": onSubmit="return checkAllValues(this)"
1. in C++ or others, function returns some value, e.g. checkAllValues() itself is a value: true or false.....why we should add a return in the above ?
2. if onSubmit="true" then submit else not submit?
Cheers.
That is the code I am interested!
About "return": onSubmit="return checkAllValues(this)"
1. in C++ or others, function returns some value, e.g. checkAllValues() itself is a value: true or false.....why we should add a return in the above ?
2. if onSubmit="true" then submit else not submit?
Cheers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Joseph,
Yes, I understand now: on submit all the values would dissapear, but with "return", the values still therer.....this "return" is not like the one in the script....I just confused those "return"s!!
Thank you for yoru excellent comments and the last comment has become an excellent answer!
Yes, I understand now: on submit all the values would dissapear, but with "return", the values still therer.....this "return" is not like the one in the script....I just confused those "return"s!!
Thank you for yoru excellent comments and the last comment has become an excellent answer!
Well, thank you too for the compliments :)
Joseph
Joseph
sub stringlength()
if len(erfform.just.value) > 100 then
msgbox ("The max length is 100!")
erfform.just.value = mid(erfform.just.value,1,1
end if
<textarea onkeypress="stringlength()
It isn't exactly what you want, but you might wanna experiment with this.
Joseph