Advertisement

03.04.2008 at 12:56PM PST, ID: 23214033
[x]
Attachment Details

Javascript form validation works in I.e., not safari or firefox

Asked by MulyCat in Safari Web Browser, JavaScript, Programming for ASP.NET

I have an asp.net application where I use a javascript file to validate the form entries (basic bill to ship to info). The .js file utilizes the class object for its tpe validation. I have created clientid's in my code and I create an array of required fields and a companion text map to populate an alert message string. All works well in I.E. In Safari the code stops working once I define the arrays. I am quite stumped. Any help would be appreciated. Code attached, sorry my own comments were not edited out - Please be kind!! This is my bosses account, so please do not hold it against him..

Thanks :)Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
 
// For a more flexible event registration routine, see
 
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    };
  }
}
 
addLoadEvent(attachFormHandlers());
addLoadEvent(function() {
  /* more code to run on page load */ 
 
 
});
function attachFormHandlers()
{
  // Ensure we're working with a 'relatively' standards 
  // compliant browser
  if (document.getElementsByTagName)
  {
 
    var objForm = window.document.getElementsByTagName('form');
    
    //objForm.innerHTML = objForm.innerHTML;
    for (var iCounter=0; iCounter<objForm.length; iCounter++)
      objForm[iCounter].onsubmit = function(){return formCheck(this);};
  }
}
function formCheck(formobj){
 
 
	// Enter name of mandatory fields
    //var fieldRequired = new Array();
   var objForm = window.document.form1.elements;
    
   if (window.document.form1.shipbilltosame.checked)
   {
	    
   //Same Bill To Ship To
	    var country = countryID.options(countryID.selectedIndex).text;
	// COUNTRY United States or Canada
	        if(country == "United States" || country == "Canada")
	        {
	        //state drop down 
	            var state = stateID.options(stateID.selectedIndex).text;
	            if (state == "- Canadian Provinces -" || state == "- US States -" || state == "Select State / Province")
	            {
	            alert ("Please select from the Bill To State Drop Down");
	            return false;
	    
	            }
    //alert ("hey " + country);
	            //var fieldRequired = Array(firstnameID.id, lastnameID.id, address1ID.id, cityID.id, stateID.id, zipID.id, email_addressID.id, phoneID.id, countryID.id);
	            var fieldRequired = [firstnameID.id, lastnameID.id, address1ID.id, cityID.id, stateID.id, zipID.id, email_addressID.id, phoneID.id, countryID.id];
	// Enter field description to appear in the dialog box
	            var fieldDescription = ["First Name", "Bill To Last Name", "Bill To First Address Line", "Bill To City", "Bill To State", "Bill To Zip", "Email Address is either blank or not valid", "Bill To Phone", "Bill To Country"];
	        }
	// COUNTRY NOT United States or Canada
	        else
	        {
	            var fieldRequired = [firstnameID.id, lastnameID.id, address1ID.id, email_addressID.id, phoneID.id, countryID.id];
	// Enter field description to appear in the dialog box
	            var fieldDescription = ["Bill To First Name", "Bill To Last Name", "Bill To First Address Line", "Email Address is either blank or not valid", "Bill To Phone", "Bill To Country"];
	//alert ("hey international" + country);
	        }
	// dialog message
	}
	else
	{
   //Different Bill To Ship To
	    //var country = countryID.options(countryID.selectedIndex).text;
	    var shipcountry = ship_countryID.options(ship_countryID.selectedIndex).text;
	     if (shipcountry == "Select Country")
	     {
	    alert ("Please make a selection from the Ship To Country or check the Ship To Same Box");
	    return false;
	    
	    }
	// COUNTRY United States or Canada
	        if (country === "United States" || country === "Canada")
	            {var BC = "dd";}
	        else
	           {var BC = "nd";}
	        
	        if (shipcountry === "United States" || shipcountry === "Canada")
	            {var SC = "dd";}
	        else
	            {var SC = "nd";}
	        //alert ("hey SC "+ SC + "BC " + BC);
	            
	            if (BC === "dd" && SC === "dd")
	                {
    //alert ("hey " + country);
	                var fieldRequired = [firstnameID.id, lastnameID.id, address1ID.id, cityID.id, stateID.id, zipID.id, email_addressID.id, phoneID.id, countryID.id, ship_firstnameID.id, ship_lastnameID.id, ship_address1ID.id, ship_cityID.id, ship_stateID.id, ship_zipID.id, ship_phoneID.id, ship_countryID.id];
	// Enter field description to appear in the dialog box
	                var fieldDescription = ["Bill To First Name", "Bill To Last Name", "Bill To First Address Line", "Bill To City", "Bill To State", "Bill To Zip", "Email Address is either blank or not valid", "Bill To Phone", "Bill To Country", "Ship To First Name", "Ship To Last Name", "Ship To First Address Line", "Ship To City", "Ship To State", "Ship To Zip", "Ship To Phone", "Ship To Country"];
	                }
	                
	// COUNTRY NOT United States or Canada
	            if (BC === "nd" && SC === "nd")
	                {
	                var fieldRequired = [firstnameID.id, lastnameID.id, address1ID.id, email_addressID.id, phoneID.id, countryID.id, ship_firstnameID.id, ship_lastnameID.id, ship_address1ID.id, ship_phoneID.id, ship_countryID.id];
	// Enter field description to appear in the dialog box
	                var fieldDescription = ["Bill To First Name", "Bill To Last Name", "Bill To First Address Line", "Email Address is either blank or not valid", "Bill To Phone", "Bill To Country", "Ship To First Name", "Ship To Last Name", "Ship To First Address Line", "Ship To Phone", "Ship To Country"];
	//alert ("hey international" + country);
	                }
	                
	            if (BC === "dd" && SC === "nd")
	                {
    //alert ("hey " + country);
	                var fieldRequired = [firstnameID.id, lastnameID.id, address1ID.id, cityID.id, stateID.id, zipID.id, email_addressID.id, phoneID.id, countryID.id, ship_firstnameID.id, ship_lastnameID.id, ship_address1ID.id, ship_phoneID.id, ship_countryID.id];
	// Enter field description to appear in the dialog box
	                var fieldDescription = ["Bill To First Name", "Bill To Last Name", "Bill To First Address Line", "Bill To City", "Bill To State", "Bill To Zip", "Email Address is either blank or not valid", "Bill To Phone", "Bill To Country", "Ship To First Name", "Ship To Last Name", "Ship To First Address Line", "Ship To Phone", "Ship To Country"];
	                }
	                
	// COUNTRY NOT United States or Canada
	            if (BC === "nd" && SC === "dd")
	                {
	                var fieldRequired = [firstnameID.id, lastnameID.id, address1ID.id, email_addressID.id, phoneID.id, countryID.id, ship_firstnameID.id, ship_lastnameID.id, ship_address1ID.id, ship_cityID.id, ship_stateID.id, ship_zipID.id, ship_phoneID.id, ship_countryID.id];
	// Enter field description to appear in the dialog box
	                var fieldDescription = ["Bill To First Name", "Bill To Last Name", "Bill To First Address Line", "Email Address is either blank or not valid", "Bill To Phone", "Bill To Country", "Ship To First Name", "Ship To Last Name", "Ship To First Address Line", "Ship To City", "Ship To State", "Ship To Zip", "Ship To Phone", "Ship To Country"];
	
	                }
	                
	// dialog message
	}
	var alertMsg = "Please complete the following fields:\n";
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
	var obj = objForm.elements[fieldRequired[i]];
	
			if (obj){
			switch(obj.className){
			//switch(obj.type){
			//case "select-one":
			case "select-one":
				if (obj.selectedIndex === -1 || obj.options[obj.selectedIndex].value === "" || obj.options[obj.selectedIndex].value === null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			//case "textarea":
				if (obj.value === "" || obj.value === null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
				case "email":
			//case "email":
				
					var x = obj.value;
	                var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	                if (!filter.test(x)){ //break;//alert('YES! Correct email address');
	                //else alertMsg += " - " + fieldDescription[i] + "\n";
	                alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "numeric":
			    
				if (obj.value === "" || obj.value === null || isNaN(obj.value)){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex === -1 || obj.options[obj.selectedIndex].value === "" || obj.options[obj.selectedIndex].value === null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
				default:
			}
			//if (obj.type == undefined){
			if (obj.className === undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}
 
 
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
 
// -->
[+][-]03.04.2008 at 06:01PM PST, ID: 21047143

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.04.2008 at 08:25PM PST, ID: 21047724

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2008 at 11:26AM PST, ID: 21063771

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.07.2008 at 09:11AM PST, ID: 21072242

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Safari Web Browser, JavaScript, Programming for ASP.NET
Sign Up Now!
Solution Provided By: MulyCat
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628