/    Sign up×
Community /Pin to ProfileBookmark

verify radio button group with testForms?

I uses a javacript to validate my forms. It uses a testForms protocol like this with elements too check being called isXXXX.

[code=html]
// Checks the NAME field.
function isName(){
var str = document.forms[0].name.value;

// Return false if first name field is blank.
if (str == “”){
alert(“nYour NAME field is blank.nnPlease re-enter your full name.”)
document.forms[0].name.focus();
return false;
}
return true;
}
[/code]

problem is, I can get it to validate drop downs and text fields, but not sure hot to modiy it to get it to validate a radio button groupp using the form check type I have.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisDec 12.2006 — To start, I would change that to something more like this:
[CODE]
// Checks the NAME field.
function isName(){

var [COLOR=Blue]fld[/COLOR] = document.forms[0].[COLOR=Blue]elements[/COLOR]['name'];

// Return false if first name field is blank.
if (/^s*$/.test([COLOR=Blue]fld.value[/COLOR])){
alert("nYour NAME field is blank.nnPlease re-enter your full name.")
[COLOR=Blue]fld[/COLOR].focus();
return false;
}
return true;
}[/CODE]

Even then, it seems you have one function per field. Using a layout similar to the above, it is possible to use the same function for many fields -- and still have automatic variety in your alert message which is unique to the individual field being validated. But that is a separate subject.

As for radio buttons, the following would validate that a radio button is clicked within the group:
[CODE]
// Checks the RADIO field.
function isRadio(){

var [COLOR=Blue]fld[/COLOR] = document.forms[0].[COLOR=Blue]elements[/COLOR]['radio'];
var x, len = fld.length;
for(x=0; x<len; ++x) if(fld[x].checked) break;

// Return false if no radio button is checked.
if (len <= x){
alert("nYour RADIO selection is missing.nnPlease make a RADIO selection.")
[COLOR=Blue]fld[0][/COLOR].focus();
return false;
}
return true;
}[/CODE]
Copy linkTweet thisAlerts:
@etardauthorDec 12.2006 — sweet! that is it.... I see now what I was doing wrong in trying to modify my code for a radio group.

thanks!
Copy linkTweet thisAlerts:
@so_is_thisDec 12.2006 — Salud!
×

Success!

Help @etard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...