I have a validation issue. I need the input fields to highlight yellow if no text has been entered and when the form is submitted the error message displays to check the highlighted fields.
I get the input boxes to highlight when there is no text but on submit the form goes to the next page without the error alert message.
Any suggestions would be appreciated.
/*
Functions List:
initForm()
Initiates the Web form for use by the customer
calcCost()
Calculates the cost of the customer order
validLengths()
Validates that empty values have been entered for required
fields
testLength(field)
Tests the length of the text string in the specified field
validPatterns()
Validates that the field values match their regular expressions
testPattern(field, reg)
Tests a text string against a specified regular expression
validCNum()
Tests that the specified credit card number passes the Luhn formula
validateForm()
Performs a validation test on all of the fields in the form
*/
window.onload = initForm;
var wform;
var productIndex = 1;
var qtyIndex = 1;
var shipIndex = 1;
function initForm() {
wform = document.forms[0];
wform.product.onchange = calcCost;
wform.qty.onchange = calcCost;
wform.shipping.onchange = calcCost;
wform.onsubmit = validateForm;
}
function calcCost() {
productIndex = wform.product.selectedIndex;
productCost = parseFloat(wform.product.options[productIndex].value);
qtyIndex = wform.qty.selectedIndex;
qtyVal = parseFloat(wform.qty.options[qtyIndex].value);
shipIndex = wform.shipping.selectedIndex;
shipVal = parseFloat(wform.shipping.options[shipIndex].value);
if (productIndex != 0 && qtyIndex != 0 && shipIndex != 0) {
wform.total.value = "$"+(productCost*qtyVal+shipVal).toFixed(2);
}
}
function validLengths() {
var isValid = true;
if (testLength(wform.total)== false) isValid = false;
if (testLength(wform.fname)== false) isValid = false;
if (testLength(wform.lname)== false) isValid=false;
if (testLength(wform.address)== false) isValid=false;
if (testLength(wform.phone)== false) isValid=false;
if (testLength(wform.cholder)== false) isValid=false;
if (testLength(wform.cnum)== false) isValid=false;
return isValid;
}
function testLength(field) {
var isValid = true;
if (field.value.length == 0) {
document.getElementById(field.id+"contact").style.bgcolor="yellow";
isValid = false;
} else {
document.getElementById(field.id+"contact").style.bgcolor="white";
}
return isValid;
}
function validPatterns() {
var isValid = true;
phonereg = /^\(?\d{3}[\)-]?\s?\d{3}[\s-]?\d{4}$/;
if (testPattern(wform.phone, phonereg) == false) isValid = false;
creditType = wform.ccard.selectedIndex;
switch (creditType) {
case 0: cregx = /^3[47]\d{13}$/; break;
case 1: cregx = /^30[0-5]\d{11}$|^3[68]\d{12}$/; break;
case 2: cregx = /^6011\d{12}$/; break;
case 3: cregx = /^5[1-5]\d{14}$/; break;
case 4: cregx = /^4(\d{12}|\d{15})$/; break;
}
if (testPattern(wform.cnum, cregx) == false) isValid = false;
return isValid;
}
function testPattern(field, reg) {
var isValid = true;
wsregx = /\s/g;
var fv =field.value.replace(wsregx,"");
if (reg.test(fv) == false) {
isValid = false;
document.getElementById(input.id+"fname").style.bgcolor="yellow";
} else {
document.getElementById(input.id+"fname").style.bgcolor="white";
}
return isValid;
}
function validCNum() {
var isValid = true;
wsregx = /\s/g;
var fv = wform.cnum.value.replace(wsregx,"");
if (luhn(fv)==false) {
isValid = false;
document.getElementById("cnum").style.bgcolor="yellow";
} else {
document.getElementById("cnum").style.bgcolor="white";
}
return isValid;
}
function changeColor(field) {
var fv = field.value;
if (fv.length==0) {
field.style.background="yellow";
} else {
field.style.background="white";
}
}
function validateForm() {
var vLengths = validLengths();
var vPatterns = validPatterns();
var vCNum = validCNum();
var vForm = vLengths && vPatterns && vCNum;
if (!vForm) {
alert("Check the highlighted items in the form for missing/invalid data");
return false;
} else {
return true;
}
}
__________________ Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
So when you try to do testLength() of the total field, which has an id of "total", you try to set the background color of something with the id of "totalcontact".
And ditto for every field that you invoke testLength() on.
And similar for testPattern, where you do worse:
Code:
function testPattern(field, reg) {
var isValid = true;
wsregx = /\s/g;
var fv =field.value.replace(wsregx,"");
if (reg.test(fv) == false) {
isValid = false;
document.getElementById(input.id+"fname").style.bgcolor="yellow";
} else {
document.getElementById(input.id+"fname").style.bgcolor="white";
}
return isValid;
}
Here, you are trying to append "fname" to input.id. But input is not even a valid variable at this point in the code.
I'm *guessing* that you meant to use field.id, but that makes no sense, either, as you don't have any elements with an id of "xxxfname" except "fname" itself.
Quite frankly, I don't understand what the point of appending these strings to these ids is. Are you missing a bunch of tags that should, indeed, have ids such as "totalcontact" and "fnamecontact" and so on???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Yes I have a mess. I got totally lost trying to get the input fields to highlight when there was a validation error. I have no idea how to fix this where it will highlight if the input is missing and display and error message stating to check highlighted fields for missing input. If all fields are filled in correctly then it should go to the done page.
Thanks for your help
Yes I have a mess. I got totally lost trying to get the input fields to highlight when there was a validation error. I have no idea how to fix this.....
Maybe consider starting your javascript again incorporating some Basic Debugging 101 as you go along - basically, code and test in small chunks.
Here's another variation that simplifies the HTML even further: No id's needed for the <label>s. Flexibiity on minimum length for text fields.
Code:
<html>
<head>
<script type="text/javascript">
function colorLabel(field,okay)
{
// find the label that contains the given field...
var node = field.parentNode;
while ( node.tagName.toLowerCase() != "label" )
{
node = node.parentNode;
if ( node == null )
{
alert("Label not found for field " + field.name);
return okay;
}
}
// and set its color according to the flag
node.style.backgroundColor = okay ? "transparent" : "pink";
// and by returning the flag, we simplify the code in the callers
return okay;
}
function validateSelect(select)
{
return colorLabel(select, select.selectedIndex != 0);
}
function validateTextLength(input,min)
{
var text = input.value.replace(/\s/g,"");
return colorLabel( input, text.length >= min );
}
// notice how we passed the reference to the <form> object into this function:
function validateForm(form)
{
// notice how we can so easily refer to the form fields by name:
var okay = validateSelect(form.product)
& validateSelect(form.qty)
& validateTextLength(form.fname, 3);
if ( okay == 0 ) alert("Please correct the indicated errors");
return okay != 0;
}
</script>
<body>
<form action="" onsubmit="return validateForm(this)" method="get">
<label> 1) Product
<select name="product">
<option value="">Select a Product</option>
<option value="3.5">Green/Purple Fountain: $3.50 ea.</option>
<option value="4.95">Silver Cone: $4.95 ea.</option>
<option value="6.95">Glitter Cone: $6.95 ea.</option>
<option value="9.95">Glittering Stars: $9.95 ea.</option>
<option value="19.95">Fountain Kit: $19.95 ea.</option>
<option value="29.95">Fountain Kit Deluxe: $29.95</option>
<option value="39.95">Giant Fountain: $39.95</option>
</select>
</label>
<br />
<label> 2) Quantity
<select name="qty">
<option value="">Select a Quantity</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
<option value="4"> 4</option>
<option value="5"> 5</option>
<option value="6"> 6</option>
<option value="7"> 7</option>
<option value="8"> 8</option>
<option value="9"> 9</option>
<option value="10">10</option>
</select>
</label>
<br/><br/>
<label> 4) First Name
<input name="fname" size="20" type="text" />
</label>
<br/><br/>
<input type="submit" value="Submit Order" />
</form>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 05-02-2011 at 12:25 AM..
if(!patt.test(ele.val())) {
jVal.errors = true;
birthInfo.removeClass('correct').addClass('error').html(' type in date in correct format').show();
ele.removeClass('normal').addClass('wrong');
} else {
birthInfo.removeClass('error').addClass('correct').html('√').show();
ele.removeClass('wrong').addClass('normal');
}
},
//-----------------------date validation-------------------------------------------------------------------->
'gender' : function (){
if($('input[name="gender"]:checked').length === 0) {
jVal.errors = true;
genderInfo.removeClass('correct').addClass('error').html('Enter Your Gender').show();//html('← are you a man or a woman?').show();
ele.removeClass('normal').addClass('wrong');
} else {
genderInfo.removeClass('error').addClass('correct').html('√').show();
ele.removeClass('wrong').addClass('normal');
}
},
//-----------------------gender validation end-------------------------------------------------------------------->
'agree' : function (){
thenm - Please do not hijack someone else's long-completed thread. Prefer to start a new thread of your own in the appropriate (jQuery) section of the forum. Please explain your problem clearly.
Before you do that, please read the forum rules and posting guidelines. When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar.
It is your responsibility to die() if necessary….. - PHP Manual
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.