Jump to content


Photo

Form Validation html/javascript


  • Please log in to reply
1 reply to this topic

#1 darko

darko
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 11 February 2006 - 10:09 AM

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

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

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

#2 Javizy

Javizy
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 12 February 2006 - 05:07 AM

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

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

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users