/    Sign up×
Community /Pin to ProfileBookmark

Validating a HTML Form

I don’t know if this is the right place but i can’t imagine I can do what i want without AJAX/Javascript. Basically I have a form a user uses to insert some data to a database. I want to make sure everything is correct and everything is inserted that needs to be and everything that needs to be unique is unique from the database. The one way i can imagine doing this is to using ajax everytime something in any of the form fields change and when everything is complete and unique, I will display a active submit button and if not i will display a submit button button an inactive one however one of them is a textarea and I am not sure it that is the best method since people can type fast and ajax maybe be a bit to slow. Is their a faster method for doing this without having to have the user fill in information submit and then i check it and tell the username any errors they made?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@EridiusauthorJun 18.2007 — anyone?

is using ajax as described the fastest way of doing what i what?
Copy linkTweet thisAlerts:
@EridiusauthorJun 19.2007 — so no one can tell me if i am doing this the right way?
Copy linkTweet thisAlerts:
@TJ111Jun 19.2007 — Try something like this. It's what I use before anyone submits a form. (Yeah I know I'm lazy). [url=http://javascript.internet.com/forms/regexp-validation.html]Source[/url]

<i>
</i>// returns true if the string is empty
function isEmpty(str){
return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
if(isEmpty(str)) return false;
var re = /^[^s()&lt;&gt;@,;:/]+@w[w.-]+.[a-z]{2,}$/i
return re.test(str);
}
// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
var re = /[^a-zA-Z]/g
if (re.test(str)) return false;
return true;
}
// returns true if the string only contains characters 0-9
function isNumeric(str){
var re = /[D]/g
if (re.test(str)) return false;
return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
var re = /[^a-zA-Z0-9]/g
if (re.test(str)) return false;
return true;
}
// returns true if the string's length equals "len"
function isLength(str, len){
return str.length == len;
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
return (str.length &gt;= min)&amp;&amp;(str.length &lt;= max);
}
// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
var re = /^(?[2-9]d{2}[).-]?s?d{3}[s.-]?d{4}$/
return re.test(str);
}
// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
var re = /^(d{1,2})[s./-](d{1,2})[s./-](d{4})$/
if (!re.test(str)) return false;
var result = str.match(re);
var m = parseInt(result[1]);
var d = parseInt(result[2]);
var y = parseInt(result[3]);
if(m &lt; 1 || m &gt; 12 || y &lt; 1900 || y &gt; 2100) return false;
if(m == 2){
var days = ((y % 4) == 0) ? 29 : 28;
}else if(m == 4 || m == 6 || m == 9 || m == 11){
var days = 30;
}else{
var days = 31;
}
return (d &gt;= 1 &amp;&amp; d &lt;= days);
}
// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
return str1 == str2;
}
// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
var re = /[S]/g
if (re.test(str)) return false;
return true;
}
// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
if (replacement == null) replacement = '';
var result = str;
var re = /s/g
if(str.search(re) != -1){
result = str.replace(re, replacement);
}
return result;
}
// validate the form
function validateForm(f, preCheck){
var errors = '';
if(preCheck != null) errors += preCheck;
var i,e,t,n,v;
for(i=0; i &lt; f.elements.length; i++){
e = f.elements[i];
if(e.optional) continue;
t = e.type;
n = e.name;
v = e.value;
if(t == 'text' || t == 'password' || t == 'textarea'){
if(isEmpty(v)){
errors += n+' cannot be empty.n'; continue;
}
if(v == e.defaultValue){
errors += n+' cannot use the default value.n'; continue;
}
if(e.isAlpha){
if(!isAlpha(v)){
errors += n+' can only contain characters A-Z a-z.n'; continue;
}
}
if(e.isNumeric){
if(!isNumeric(v)){
errors += n+' can only contain characters 0-9.n'; continue;
}
}
if(e.isAlphaNumeric){
if(!isAlphaNumeric(v)){
errors += n+' can only contain characters A-Z a-z 0-9.n'; continue;
}
}
if(e.isEmail){
if(!isEmail(v)){
errors += v+' is not a valid email.n'; continue;
}
}
if(e.isLength != null){
var len = e.isLength;
if(!isLength(v,len)){
errors += n+' must contain only '+len+' characters.n'; continue;
}
}
if(e.isLengthBetween != null){
var min = e.isLengthBetween[0];
var max = e.isLengthBetween[1];
if(!isLengthBetween(v,min,max)){
errors += n+' cannot contain less than '+min+' or more than '+max+' characters.n'; continue;
}
}
if(e.isPhoneNumber){
if(!isPhoneNumber(v)){
errors += v+' is not a valid US phone number.n'; continue;
}
}
if(e.isDate){
if(!isDate(v)){
errors += v+' is not a valid date.n'; continue;
}
}
if(e.isMatch != null){
if(!isMatch(v, e.isMatch)){
errors += n+' does not match.n'; continue;
}
}
}
if(t.indexOf('select') != -1){
if(isEmpty(e.options[e.selectedIndex].value)){
errors += n+' needs an option selected.n'; continue;
}
}
if(t == 'file'){
if(isEmpty(v)){
errors += n+' needs a file to upload.n'; continue;
}
}
}
if(errors != '') alert(errors);
return errors == '';
}

/*
The following elements are not validated...

button type="button"
checkbox type="checkbox"
hidden type="hidden"
radio type="radio"
reset type="reset"
submit type="submit"

All elements are assumed required and will only be validated for an
empty value or defaultValue unless specified by the following properties.

isEmail = true; // valid email address
isAlpha = true; // A-Z a-z characters only
isNumeric = true; // 0-9 characters only
isAlphaNumeric = true; // A-Z a-z 0-9 characters only
isLength = number; // must be exact length
isLengthBetween = array; // [lowNumber, highNumber] must be between lowNumber and highNumber
isPhoneNumber = true; // valid US phone number. See "isPhoneNumber()" comments for the formatting rules
isDate = true; // valid date. See "isDate()" comments for the formatting rules
isMatch = string; // must match string
optional = true; // element will not be validated
*/

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// --------------------------------------------------
// ||||||||||||||||||||||||||||||||||||||||||||||||||

// All of the previous JavaScript is coded to process
// any form and should be kept in an external file if
// multiple forms are being processed.

// This function configures the previous
// form validation code for this form.
function configureValidation(f){
f.firstname.isAlphaNumeric = true;
f.lastname.isAlphaNumeric = true;
f.email.isEmail = true;
f.phone.isPhoneNumber = true;
f.birthday.isDate = true;
f.password1.isLengthBetween = [4,255];
f.password2.isMatch = f.password1.value;
f.comments.optional = true;
var preCheck = (!f.infohtml.checked &amp;&amp; !f.infocss.checked &amp;&amp; !f.infojs.checked) ? 'select at least one checkbox.n' : null;
return validateForm(f, preCheck);
}
×

Success!

Help @Eridius 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.1,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...