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)
learnAsked:
Who is Participating?
 
oubelkasConnect With a Mentor Commented:
Well, look, return here must be said to hold the values earlier given. So if you remove the keyword return here and the checkform is being called because a wrong entry was given, all the values will be reset. With return, the values stay intact. Try it out. Remove the return and see what happens. Oh, here two things you might want to add, check for checkbox or radiobutton. Add this javascript :

 if (!myForm.myCheckbox.checked){
     alert('Check this!');
     myForm.myCheckbox.focus();
     return (false);
  }
  if (!myForm.myRadio.checked){
     alert('Check this radio button too!!');
     myForm.myRadio.focus();
     return (false);
  }

then add this in the table :

 <tr>
     <td valign="top" align="right"><b>Check this<b></td>
     <td valign="top" align="left"><input type="checkbox" name="myCheckbox" value="1"></td>
   </tr>
    <tr>
     <td valign="top" align="right"><b>Check this<b></td>
     <td valign="top" align="left"><input type="radio" name="myRadio" value="1"></td>
    </tr>

Submit will be executed once pushed on the button.

Hope it's clear to you now. :)

Joseph


0
 
oubelkasCommented:
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
0
 
freshmeatCommented:
<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
:)

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
learnAuthor Commented:
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?
0
 
learnAuthor Commented:
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>
0
 
oubelkasCommented:
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
0
 
brunoCommented:
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
0
 
oubelkasCommented:
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
0
 
learnAuthor Commented:
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.
0
 
learnAuthor Commented:
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!
0
 
oubelkasCommented:
Well, thank you too for the compliments :)

Joseph
0
All Courses

From novice to tech pro — start learning today.