/    Sign up×
Community /Pin to ProfileBookmark

Where to learn form validation from beginning?

I am trying to learn form validate techniques and to use technology in advance mode. In order to do this I have first learn the mentioned technique from the beginning so I can apply it as desired. I am using a form with asp linked database[simple html form with a asp script for the database connections].

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMay 23.2006 — Complete JavaScript form validation is done by combining two levels of client-side validation -- field level and form level -- with normal server-side validation (via ASP or PHP, etc). Note that it is important to include server-side validation because there are ways that your client-side validation can be bypassed (e.g., by spammers). At any rate...

Field level validation is done by using each field's [b]onchange[/b] event as follows:

onchange="return fieldValidation(this)"

Form level validation is done by using the FORM tag's [b]onsubmit[/b] event as follows:

onsubmit="return formValidation(this)"

Both event handlers (functions) should be written using this overall format:
function eventHandler(obj) {
...
if(validation fails) {
...
return false;
}
...
return true;
}

The key parts of this format are the [b]return[/b] statements. Returning [b]false[/b] to an event cancels the default action of that event. In the case of [b]onchange[/b], this event fires when the focus is being removed from a form element that has been changed. The default action is to allow the focus to be removed. Thus, cancelling this default action keeps the focus on the form element that is in error. In the case of [b]onsubmit[/b], this event fires when the form is submitted. The default action is to allow the form to continue with the submit. Thus, cancelling this default action keeps the form from submitting in the case of errors. (Note that this event does [B][I][U]not[/U][/I][/B] fire if you incorporate the use of the form's [b]submit()[/b] method in your code.)

Now... A more fleshed out version of the field level validation function would look something like this (this one validating merely for required content):
function fieldValidation(fld) {
if(/^s*$/.test(fld.value)) {
alert(fld.name + " is required.");
if(fld.select) fld.select();
return false;
}
return true;
}

Lastly, the way that you combine these two validation levels is to also call your field level validation functions from your form level validation function. Thus, the following is a more fleshed out version of the form validation function:
function formValidation(f) {
var fld;
if(!fieldValidation(fld=f.elements["myField"])) {
fld.focus();
return false;
}
return true;
}

Any questions? ?
×

Success!

Help @Shaz444 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.8,
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,
)...