473,388 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Validation of text field upon radio button selection

Hey Guys,

Please could anyone help me out with the following form I need to create a validation on the email field only if the user checked the radio button named Email.

Thanks in advance.


[HTML]<form action="" method="get">
<div align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Name:</td>
<td>
<input type="text" name="textfield" id="textfield" />
</td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="textfield2" id="textfield2" /></td>
</tr>
<tr>
<td>E-mail Address:</td>
<td><input type="text" name="textfield3" id="textfield3" /></td>
</tr>
<tr>
<td>Contact Method:</td>
<td>
<input type="radio" name="radio" id="radio" value="radio" /> Phone <input type="radio" name="radio" id="radio" value="radio" />
Email
</td>
</tr>
<tr>
<td>Type of Inquiry:</td>
<td><label><select name="select" id="select" >
<option selected="selected">Question</option>
<option >Comment</option>
</select></label></td>
</tr>
<tr>
<td>Content:</td>
<td><textarea name="textarea" id="textarea" cols="45" rows="5"></textarea></td>
</tr>
</table>
<input name="Submit" type="button" value="Submit Form" />
</div></form>[/HTML]
Sep 17 '08 #1
4 3008
stepterr
157 100+
Hey Guys,

Please could anyone help me out with the following form I need to create a validation on the email field only if the user checked the radio button named Email.

Thanks in advance.


<form action="" method="get">
<div align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Name:</td>
<td>
<input type="text" name="textfield" id="textfield" />
</td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="textfield2" id="textfield2" /></td>
</tr>
<tr>
<td>E-mail Address:</td>
<td><input type="text" name="textfield3" id="textfield3" /></td>
</tr>
<tr>
<td>Contact Method:</td>
<td>
<input type="radio" name="radio" id="radio" value="radio" /> Phone <input type="radio" name="radio" id="radio" value="radio" />
Email
</td>
</tr>
<tr>
<td>Type of Inquiry:</td>
<td><label><select name="select" id="select" >
<option selected="selected">Question</option>
<option >Comment</option>
</select></label></td>
</tr>
<tr>
<td>Content:</td>
<td><textarea name="textarea" id="textarea" cols="45" rows="5"></textarea></td>
</tr>
</table>
<input name="Submit" type="button" value="Submit Form" />
</div></form>

pureadrenaline,
First thing you will need to do is set the value of the radio button to something unique so that you can tell if they selected "phone" or "email". Currently you have the value for both of them set to "radio".

Now, to get the value of the radio button you'll need to iterate through all the radio buttons in that set, those that have the same name, and then get the value from the one that is checked.

Here's an example of this:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript>
  2. <!--
  3.  
  4. function get_radio_value()
  5. {
  6. for (var i=0; i < document.formname.radio.length; i++)
  7.    {
  8.    if (document.formname.radio[i].checked)
  9.       {
  10.       var rad_val = document.formname.radio[i].value;
  11.       }
  12.    }
  13. }
  14.  
  15. //-->
  16. </script>
Now you can check what rad_val is equal to and if it is "email" then you can do your email validation.
Sep 17 '08 #2
ok, Could you please give me the whole code of the page?

Thanks a lot.
Sep 17 '08 #3
stepterr
157 100+
ok, Could you please give me the whole code of the page?

Thanks a lot.
Here's an example to get you started. You can expand upon it further. In the example I've changed the field names as I find it helpful to name them something meaningful, especially if you will be doing validation on them.

The form (only with the fields in question for this example):
Expand|Select|Wrap|Line Numbers
  1. <form name="contactform" method="post">
  2. <table border="0" cellspacing="0" cellpadding="0" align="center">
  3.     <tr>
  4.         <td>Phone Number:</td>
  5.         <td><input type="text" name="Phone" id="Phone" /></td>
  6.     </tr>
  7.     <tr>
  8.         <td>E-mail Address:</td>
  9.         <td><input type="text" name="Email" id="Email" /></td>
  10.     </tr>
  11.     <tr>
  12.         <td>Contact Method:</td>
  13.         <td>
  14.         <input type="radio" name="Method" id="Method" value="Phone" />Phone 
  15.         <input type="radio" name="Method" id="Method" value="Email" />Email 
  16.         </td>
  17.     </tr>
  18. </table>
  19. <input name="Submit" type="button" value="Submit Form" onclick="get_radio_value()" />
  20. </form>

The javascript:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.  
  3. var f=document.contactform;
  4.  
  5. function get_radio_value()
  6. {
  7.    for (var i=0; i < f.Method.length; i++)
  8.    {
  9.       if (f.Method[i].checked)
  10.       {
  11.           var rad_val = f.Method[i].value;
  12.            if(rad_val == "Email")
  13.          {
  14.              if(bad_email(f.Email.value))
  15.              {
  16.                 alert("Sorry, You entered an invalid email address.");
  17.                 f.Email.focus();
  18.                 return false;
  19.             }
  20.          }
  21.       }
  22.    }
  23. }
  24.  
  25. function bad_email(emailaddr)
  26. {
  27.     var emailFilter=/^.+@.+\..{2,3}$/;
  28.     if (!(emailFilter.test(emailaddr)))
  29.     {
  30.            return true;
  31.     }
  32.     var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
  33.     if (emailaddr.match(illegalChars))
  34.     {
  35.           return true;
  36.     }
  37.     return false;
  38. }
  39. </script>
This is just a basic example and it's only doing the validation for you at this point. Don't forget that once your validation passes, you'll actually want this form to submit to something, at least I'm assuming you would. :-)
Sep 17 '08 #4
acoder
16,027 Expert Mod 8TB
Note that IDs must be unique. If it's the email button you want, give it a unique ID, e.g. "email", and then access using document.getElementById().

PS. please use code tags when posting code.
Sep 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
5
by: EviL KerneL | last post by:
Hi - I am trying to figure out a way to enforce the validation included for this form based on whether the user chooses "email" or "phone" as the contact choice. Right now it is set to enforce...
3
by: alexz | last post by:
I want to check information is entered in the form with javascrip, based on a radio button or a checkbox. For example I have 2 radio buttons Yes and No. If "Yes" is click then check if the fname...
10
by: Steve Benson | last post by:
Our regular programmer moved on. I'm almost clueless in Javascript/ASP and got the job of adapting existing code. In the page below, everything works until I added the function checkIt() to...
5
by: Mattyw | last post by:
Hi, I'm relatively new to Web Forms, I have been using Required Field Validators and Regular Expression Validators on a Web Form I am developing and everything works as expected using Visual...
1
by: FunkHouse9 | last post by:
I'm trying to develop an order page and in one section, the customer specifies a shipment type using radio buttons that is submitted to the shopping cart. There are 4 buttons. If either of the...
3
by: ajitkumar071 | last post by:
hi all .....please help me ... my problem is i m having a row by default.And validation works fine for this, But if i add an extra row to it.....even if i enter the values for the new added row i...
0
by: Gary W. Smith | last post by:
Hello, I have a simple form with a radio button group on it and I need to do some basic validation based upon which button is checked. Here is a little more detail. if button one is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.