Connecting Tech Pros Worldwide Help | Site Map

Inline error with radio buttons

fieryscream's Avatar
Newbie
 
Join Date: Sep 2009
Location: NZ
Posts: 8
#1: Sep 2 '09
This is the javascript code that i have for validating a form, it raises error if the user puts in invalid information. The problem im having is when i enter:
Expand|Select|Wrap|Line Numbers
  1. /*  if(title.checked=false){
  2.   inlineMsg('radio', 'You must select a title.',2);
  3.   return false;
  4.   */
it brakes the code, but when i comment out the rest works fine?????

Expand|Select|Wrap|Line Numbers
  1. function validate(form) {
  2.   var name = form.name.value;
  3.   var email = form.email.value;
  4.   var title = form.title.value;
  5.   var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  6.   var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  7.   var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  8. /*  if(title.checked=false){
  9.   inlineMsg('radio', 'You must select a title.',2);
  10.   return false;
  11.   }*/
  12.   if(name == "") {
  13.     inlineMsg('name','You must enter your name.',2);
  14.     return false;
  15.   }
  16.   if(!name.match(nameRegex)) {
  17.     inlineMsg('name','You have entered an invalid name.',2);
  18.     return false;
  19.   }
  20.   if(email == "") {
  21.     inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
  22.     return false;
  23.   }
  24.   if(!email.match(emailRegex)) {
  25.     inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
  26.     return false;
  27. }
  28.  
  29.   return true;
  30. }
HTML
Here is Html for the radio buttons
Expand|Select|Wrap|Line Numbers
  1.     <label>Mr:
  2.         <input name="title"  value="Mr"  type="radio" />
  3.       </label>
  4.       <label> Ms:
  5.         <input name="title" value="Ms" type="radio" />
  6.       </label>
  7.       <label> Mrs:
  8.         <input name="title"  value="Mrs" type="radio" />
  9.       </label>
  10.       <label> Dr:
  11.         <input name="title" value="Dr" type="radio" />
  12.       </label>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Sep 2 '09

re: Inline error with radio buttons


Quote:

Originally Posted by fieryscream View Post

Expand|Select|Wrap|Line Numbers
  1. /*  if(title.checked = false){
  2.   inlineMsg('radio', 'You must select a title.',2);
  3.   return false;
  4.   */

a less error-prone approach to if conditions:
Expand|Select|Wrap|Line Numbers
  1. if ("value" == variable)
if you’re incidentally using the assignment operator (=), there will be an error thrown
fieryscream's Avatar
Newbie
 
Join Date: Sep 2009
Location: NZ
Posts: 8
#3: Sep 3 '09

re: Inline error with radio buttons


yup i have tried the == ... but is still not works ?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Sep 3 '09

re: Inline error with radio buttons


what does the error state?
fieryscream's Avatar
Newbie
 
Join Date: Sep 2009
Location: NZ
Posts: 8
#5: Sep 6 '09

re: Inline error with radio buttons


um i got it to work in the end
however i had to make the focus of the error on another element
so i was thinking of making an invisible element next to the radio buttons in order to make the message appear next to them.

So ... how do i make a text box be invisible?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Sep 6 '09

re: Inline error with radio buttons


The error was caused by this line:
Expand|Select|Wrap|Line Numbers
  1. var title = form.title.value;
It's got the value of the radio button, not the button itself.

To hide an element, set its display property to "none" (to remove from the flow) or its visibility to "hidden".
Reply

Tags
inline error, radiobutton