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: - /* if(title.checked=false){
-
inlineMsg('radio', 'You must select a title.',2);
-
return false;
-
*/
it brakes the code, but when i comment out the rest works fine????? - function validate(form) {
-
var name = form.name.value;
-
var email = form.email.value;
-
var title = form.title.value;
-
var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
-
var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
-
var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
-
/* if(title.checked=false){
-
inlineMsg('radio', 'You must select a title.',2);
-
return false;
-
}*/
-
if(name == "") {
-
inlineMsg('name','You must enter your name.',2);
-
return false;
-
}
-
if(!name.match(nameRegex)) {
-
inlineMsg('name','You have entered an invalid name.',2);
-
return false;
-
}
-
if(email == "") {
-
inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
-
return false;
-
}
-
if(!email.match(emailRegex)) {
-
inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
-
return false;
-
}
-
-
return true;
-
}
HTML
Here is Html for the radio buttons -
<label>Mr:
-
<input name="title" value="Mr" type="radio" />
-
</label>
-
<label> Ms:
-
<input name="title" value="Ms" type="radio" />
-
</label>
-
<label> Mrs:
-
<input name="title" value="Mrs" type="radio" />
-
</label>
-
<label> Dr:
-
<input name="title" value="Dr" type="radio" />
-
</label>
5 3148 @fieryscream
a less error-prone approach to if conditions:
if you’re incidentally using the assignment operator (=), there will be an error thrown
yup i have tried the == ... but is still not works ?
what does the error state?
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?
The error was caused by this line: - 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".
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Oscar Monteiro |
last post by:
I Have to sets of Radio buttons like so:
<input type="radio" name=p1 value=1>
<input type="radio" name=p1 value=2>
<input type="radio" name=p1 value=3>
<br>
<input type="radio" name=p2 value=1>...
|
by: Craig Keightley |
last post by:
I have a page that has n number of radio groups (yes/No)
how can i prevent the form being submitted if more than one radio group is
not selected?
By default all radio groups are unchecked
...
|
by: michael |
last post by:
apologies in advance, as not only am i new to learning how to code
javascript properly, i'm new to the groups posting thing...
i am developing in firefox 1.0+, but will be working in an msie 6.0+...
|
by: Will Pittenger |
last post by:
Does C# inline functions? I do not see a inline keyword. Is there an
implicit inline? Can the compiler select functions for auto-inlining? I am
more used to C++ where all these things are...
|
by: James P. |
last post by:
Help,
I need to display radio buttons on a form. The data is from SQL
table: each row in each table is displayed as a radio button. I have
multiple SQL tables so I understand I need to put...
|
by: kenny8787 |
last post by:
Hi, can anyone help here?
I have the following code generated from a database, I want to have javascript calculate the
costs of the selected items using radio buttons, subtotal the costs and...
|
by: cpnet |
last post by:
I have a GridView which I'm populating from an ObjectDataSource (give the
GridView a DataTable). The GridView will have about 20 rows, and only one
editable column. The editable column consists...
|
by: dpazza |
last post by:
Hi,
I'm creating a quiz on using a form in VB 2005 express.
I have four sets of questions and answers (labels and radio buttons)
and I change between which set of questions is currently shown on...
|
by: gengyue |
last post by:
Hi,
I am running a report using Cold Fusion. This report is generated by cfloop tag. Depends on the record count of a query, the report will list the data retrieved from the database. each row in...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |