JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old October 25th, 2006, 05:55 AM
rkoya rkoya is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 1 rkoya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 43 sec
Reputation Power: 0
Form validation

Hi,

I have got a form which contains text fields and drop downs. I want a client side validation script which alerts the user all the errors in one alert when they click submit. I also want them to see the errors on the actual screen. It uses regular expressions to validate script.

I have so far managed to alert user all the errors and display on the screen. It uses regular expressions to validate script. However, I have not managed to incorporate the error message for the drop down into one alert for all errors. Can anyone help.

Here is the javascript:

var validationSet = {


'fldMainDriverFirstName': {
'regexp': /^[a-zA-Z]+$/,
'error': '- Please enter the name of the main driver.'
},

'fldMainDriverLastName': {
'regexp': /^[a-zA-Z]+$/,
'error': '- Please enter the surname of the main driver.'
},

'fldHouseNo': {
'regexp': /^[a-zA-Z0-9\s]+$/,
'error': '- Please enter your street name with house number.'
}
}

var fV = {
addEvent: function(elm, evType, fn, useCapture) {
// cross-browser event handling for IE5+, NS6 and Mozilla
// By Scott Andrew
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
},

init: function() {
for (var i in validationSet) {
if (document.getElementsByName(i)) {
var formField = document.getElementsByName(i)[0];
fV.addEvent(formField, 'blur', fV.checkValid, false);

if (!formField.form.validateSubmit) {
fV.addEvent(formField.form, 'submit', fV.checkValidSubmit, false);
formField.form.onsubmit = fV.checkSubmit; // Safari
formField.form.validateSubmit = true;
}
}
}
},

checkValidSubmit: function(e) {
var frm = window.event ? window.event.srcElement : e ? e.target : null;
if (!frm) return;
var errText = [];

for (var i = 0; i < frm.elements.length; i++) {
if (frm.elements[i].name && validationSet[frm.elements[i].name]) {

var failedE = fV.handleValidity(frm.elements[i]);

var errDisplay = document.getElementById('error_' + frm.elements[i].name);

if (failedE && errDisplay) {
errDisplay.innerHTML =
validationSet[failedE.name]['error'];
}
if (!failedE && errDisplay) {
errDisplay.innerHTML = '';
}

if (failedE) {
var labels = document.getElementsByTagName('label');
errText[errText.length] = validationSet[failedE.name]['error'];
for (var j = 0; j < labels.length; j++) {
if (labels[j].htmlFor == failedE.id) {
errText[errText.length - 1] +=
' (field \'' + labels[j].firstChild.nodeValue + '\')';
}

}
}
} /* end 'if' */
} /* end 'for' */


if (errText.length > 0) {
alert('___________________________________________ _______________\n' +
'Please fix the following errors and resubmit:\n\n' +
errText.join('\n') + '\n_______________________________________________ ___________\n' +
"Please re-enter and submit again");
frm.submitAllowed = false;

if (e && e.stopPropagation && e.preventDefault) {
e.stopPropagation();
e.preventDefault();
}

if (window.event) {
window.event.cancelBubble = true;
window.event.returnValue = false;
return false;
}
} else {
frm.submitAllowed = true;
}

},

checkSubmit: function() {
if (this.attachEvent) return true;
return this.submitAllowed;
},

checkValid: function(e) {
var target = window.event ? window.event.srcElement : e ? e.target : null;
if (!target) return;

var failedE = fV.handleValidity(target);

var errDisplay = document.getElementById('error_' + target.name);

if (failedE && errDisplay) {
errDisplay.innerHTML = validationSet[failedE.name]['error'];
failedE.focus();
}
if (failedE && !errDisplay) {
alert(validationSet[failedE.name]['error']);
}
if (!failedE && errDisplay) {
errDisplay.innerHTML = '';
}
},

handleValidity: function(field) {
if (!field.value) {
}


var re = validationSet[field.name]['regexp'];
if (!field.value.match(re)) {
return field;
} else {
return null;
}
}


}

fV.addEvent(window, 'load', fV.init, false);


HTML:


<form name="test" action="">
<p><label for="fldMainDriverTitle">Driver title</label>
<select id="fldMainDriverTitle" name="fldMainDriverTitle" >
<option value="none"></option>
<option value="Mr.">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
<option value="Prof">Prof</option>
<option value="Rev">Rev</option>
<option value="Sir">Sir</option>
<option value="Lady">Lady</option>
<option value="Lord">Lord</option>
</select>
<span class="price">*</span>
<span id="error_fldMainDriverTitle" class="errormessage"></span></p>

<p><label for="fldMainDriverFirstName">Driver name</label>
<input name="fldMainDriverFirstName" type="text" id="fldMainDriverFirstName" value="(MAINDRIVER_FIRSTNAME)" size="15"> <span class="price">*</span>
<span id="error_fldMainDriverFirstName" class="errormessage"></span></p>

<p><label for="fldMainDriverLastName">Driver surname</label>
<input name="fldMainDriverLastName" type="text" id="fldMainDriverLastName" value="(MAINDRIVER_LASTNAME)" size="25"> <span class="price">*</span>
<span id="error_fldMainDriverLastName" class="errormessage"></span></p>
<p><label for="fldHouseNo">Street</label>
<input name="fldHouseNo" type="text" id="fldHouseNo" value="(ADDRESS_1)" size="30">
<span class="price">*</span>
<span id="error_fldHouseNo" class="errormessage"></span></p>


Thanks

Reply With Quote
  #2  
Old October 25th, 2006, 01:50 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
Would you mind clicking "Edit Post" and wrapping your code in [code][/code] tags?
This will preserve indentation andh elp us view your code.

"I have not managed to incorporate the error message for the drop down"
i'm not sure I understand that... why validate the drop down?

Then again, I might have a better idea when I actually read your code (hope you indent )
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter!
DevArticles Forum Moderator

"The net is a waste of time, and that's exactly what's right about it." -- William Gibson

Reply With Quote
  #3  
Old October 25th, 2006, 03:35 PM
Mittineague's Avatar
Mittineague Mittineague is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 541 Mittineague User rank is Private First Class (20 - 50 Reputation Level)Mittineague User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 2 h 15 m 6 sec
Reputation Power: 3
combining errors

Hi rkoya, welcome to the forums,
I didn't study your code (please edit and wrap in BB tags as MadCowDzz mentioned) but if I understand you, this is how I would do it.
  • create a var to hold error msgs
  • when error is found append to var's val
  • when all error checks are done, if var != '' alert

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Form validation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway