Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Validation

Status
Not open for further replies.

DoctorGonzo

Programmer
Jan 23, 2004
48
GB
Hi All,

Struggling somewhat here. I use the following code on a form (which works)

function SubmitDocument(){
var frm=document.forms[0];
if(frm.QuestionSeventeen.value=="") {
alert("Please enter your postcode");
window.document.forms[0].QuestionSeventeen.focus();
return false;
}
else
document.forms[0].submit();
}

This is fine, because QuestionSeventeen is a text field. However, I have a number of radio and checkbox values on my form which I need to make sure are not NULL.

I want to make this more efficient and not use LOTS of these nested IFs.

i.e., does anyone have any code that will check firstly specified text fields, then radio buttons, then checkboxes in a more efficient manner?

I'd appreciate any help, as I couldn't seem to find a solution like this in the archive here.

Many thanks,

Gonzo.

Res Ipsa Loquitur
 
Basic radiobutton check:

Code:
var r = document.forms['formname'].elements['radiosetname'];
var isOneSelected = false;
for (var i = 0; i < r.length; i++) {
    if (r[i].checked) isOneSelected = true;
}

if (!isOneSelected) {
    alert("You must choose at least one radio button!");
    return false;
}

Checkbox validation can be done in relatively the same manner.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Thanks clFlava, I'll check that out.

Basically trying to achieve the following in a function

Function validate()
- make array of text fields, loop through them checking not null THEN
- make array of checkbox fields, loop through them checking not null THEN
- make array of radio button fields, loop through them checking not null THEN
If all OK, save doc otherwise alert that field needs to be filled in then gocus on that field
END FUNCTION

I think I have MANY radiosets (is this where it says NAME in the HTML?), so doing them one at a time would probably be as long as the previous way. Trying to get the quickest/most efficient possible (except I am useless at Javascript!)

I'll look up the elements bit in my Jscript book!

Thanks again

Gonzo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top