Jump to content

Form Validation html/javascript


darko

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
Link to comment
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 :-)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.