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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
oubelkasCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.