/    Sign up×
Community /Pin to ProfileBookmark

Form Validation

I have 4 fields. If the user fillin one, then, they all must.
I have this function and it’s working good:

[code]
var count = 0;
if ( document.registration.fld1.value != “” ) ++count;
if ( document.registration.fld2.value != “” ) ++count;
if ( document.registration.fld3.value != “” ) ++count;
if ( document.registration.fld4.value != “” ) ++count;
if ( count != 0 && count != 4 )
{
alert(“All details has to be complete”);
return false;
}
[/code]

My problem is that this function will tell me if one of’em is empty BUt I need to check the value inside each field as well. Check for numbers, dte and so on… How can I do it?

Thanks!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonJan 27.2005 — Try this:function stringVal(str){

return ( conditionA && conditionB && conditionC ) // Your validations in here

}

var count=0;

if( document.registration.fld1.value.length>0 ){
if( stringVal(document.registration.fld1.value) ){ count++; }
}
if( document.registration.fld2.value.length>0 ){
if( stringVal(document.registration.fld1.value) ){ count++; }
}
if( document.registration.fld3.value.length>0 ){
if( stringVal(document.registration.fld1.value) ){ count++; }
}
if( document.registration.fld4.value.length>0 ){
if( stringVal(document.registration.fld1.value) ){ count++; }
}

if ( count == 0 || count == 4 ){
alert("All details have to be complete!");
}
Copy linkTweet thisAlerts:
@weeeauthorJan 27.2005 — Thanks
Copy linkTweet thisAlerts:
@David_HarrisonJan 27.2005 — Happy to help. ?
×

Success!

Help @weee 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.18,
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,
)...