Link to home
Start Free TrialLog in
Avatar of learn
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:myEmailAddress"
 onsubmit="return checkform(this)">

(If need script, tell me the simple VBScript code)
Avatar of oubelkas
oubelkas

Well, to be exact, I know exactly how to do this with javascript, but not with vbscript. Though you might wanna use this :

sub stringlength()
if len(erfform.just.value) > 100 then
   msgbox ("The max length is 100!")
   erfform.just.value = mid(erfform.just.value,1,100)
end if

<textarea onkeypress="stringlength()" rows="7" name="just" cols="48"> </textarea>

It isn't exactly what you want, but you might wanna experiment with this.

Joseph
<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:myEmailAddress" > 
....
<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
:)

Avatar of learn

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?
Avatar of learn

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:myEmailArress">
<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
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
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
Avatar of learn

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.
ASKER CERTIFIED SOLUTION
Avatar of oubelkas
oubelkas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of learn

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!
Well, thank you too for the compliments :)

Joseph