Jump to content

Archived

This topic is now archived and is closed to further replies.

darko

Form Validation html/javascript

Recommended Posts

hey guys i need a a script that validates forms i'm using the following script

[code]<script language="javascript">

function form_Validator(form)
{

  if (form.name.value == "")
  {
    alert("Please enter your Full Name.");
    form.name.focus();
    return (false);
     }

  if (form.email.value == "")
  {
    alert("Please enter your email.");
    form.email.focus();
    return (false);
  }
  
    if (form.comments.value == "")
  {
    alert("Please enter your query.");
    form.comments.focus();
    return (false);
  }

  return (true);
  }
  //-->
  </script>
  
<form action="mail.php" method="post" onSubmit="return form_Validator(this)">
  <div align="center"><span class="style5">Your Full Name:<br><br></span></div>
  <center><input name="name" type="text" size="40" maxlength="40">
  </center>
<div align="center"><br>
    <span class="style5">Your E-mail:<br><br></span></div>
<center> <input name = "email" type="text" size="40" maxlength="40">
</center>
<div align="center"><br>
    <span class="style5">Please specify a Subject:</span><br>
    <br></div>
<center><select name="subject">
  <option>Ick an option</option>
  <option>Food</option>
  <option>Cars</option>
  <option>Other</option>
</select></center>
<div align="center"><br><br>
    <span class="style5">Comments:</span><br>
    <textarea name="comments" cols="30" rows="8"></textarea>
    <br>
    <br>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
</div>
</form>[/code]

----------------------------------------------------------------------------------------
the problem is i don't know how validate if the user has specified one of the options. and

lets say i had a radion button, i want to disable the submit button 'till the user clciks on

one of the radion button, (like for example he has to choose the I Agree button for a TOS)-

--i hope that makes sense, and please help me

Thanks
Darko

Share this post


Link to post
Share on other sites
[!--quoteo(post=344805:date=Feb 11 2006, 10:09 AM:name=darko)--][div class=\'quotetop\']QUOTE(darko @ Feb 11 2006, 10:09 AM) [snapback]344805[/snapback][/div][div class=\'quotemain\'][!--quotec--]
hey guys i need a a script that validates forms i'm using the following script

[code]<script language="javascript">

function form_Validator(form)
{

  if (form.name.value == "")
  {
    alert("Please enter your Full Name.");
    form.name.focus();
    return (false);
     }

  if (form.email.value == "")
  {
    alert("Please enter your email.");
    form.email.focus();
    return (false);
  }
  
    if (form.comments.value == "")
  {
    alert("Please enter your query.");
    form.comments.focus();
    return (false);
  }

  return (true);
  }
  //-->
  </script>
  
<form action="mail.php" method="post" onSubmit="return form_Validator(this)">
  <div align="center"><span class="style5">Your Full Name:<br><br></span></div>
  <center><input name="name" type="text" size="40" maxlength="40">
  </center>
<div align="center"><br>
    <span class="style5">Your E-mail:<br><br></span></div>
<center> <input name = "email" type="text" size="40" maxlength="40">
</center>
<div align="center"><br>
    <span class="style5">Please specify a Subject:</span><br>
    <br></div>
<center><select name="subject">
  <option>Ick an option</option>
  <option>Food</option>
  <option>Cars</option>
  <option>Other</option>
</select></center>
<div align="center"><br><br>
    <span class="style5">Comments:</span><br>
    <textarea name="comments" cols="30" rows="8"></textarea>
    <br>
    <br>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
</div>
</form>[/code]

----------------------------------------------------------------------------------------
the problem is i don't know how validate if the user has specified one of the options. and

lets say i had a radion button, i want to disable the submit button 'till the user clciks on

one of the radion button, (like for example he has to choose the I Agree button for a TOS)-

--i hope that makes sense, and please help me

Thanks
Darko
[/quote]

Give your submit button an id, say id="submit". Create a function that simply gets submit and does submit.disabled = "true", and call it from <body onLoad="myDisableFunction();">. This will ensure that users who don't have javascript will still be able to use your form.

Then, create another function called enableSubmit or something, which calls your validator method, and sets submit.disabled to the value it returns.

Call this function onChange for all of your form elements, and when the validation is passed, the submit button will be enabled.

Hope this helps. I might use this idea for my forum submits actually :-)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.