/    Sign up×
Community /Pin to ProfileBookmark

Validation problem in MSIE

Can anyone spot what I’m doing wrong in the following code.

Works fine in FF without any errors. ?

Does not work in MSIE, and does give any hint of errors either. ?

[code]
<html>
<head>
<title>Form Validation</title>

<script type=”text/javascript”>
// See: http://www.webdeveloper.com/forum/showthread.php?t=199694&highlight=validate+form
// and: http://fundamentaldisaster.blogspot.com/2007/10/javascript-dom-and-humble-form.html

function isRBtnChecked(radio_set) {
var sel = document.getElementsByName(radio_set);
var fnd = -1;
var str = ”;
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above
return str;
}

function isEmpty(strfield) {
var flag = false;
if (strfield == “” || strfield == null || strfield.charAt(0) == ‘ ‘) { flag = true; }
return flag;
}

function formCheck() {
var forms = document.getElementById(‘my_form’); // doesn’t work in MSIE
// var forms = document.forms[0]; // No better in MSIE

var ErrMsg = []; // alert(‘ErrMsg initial size: ‘+ErrMsg.length);
var flag = true;
if (isRBtnChecked(‘level’) == “”) { ErrMsg.push(‘Please enter “Title”‘); }

if (isEmpty(forms.elements[“firstname”].value)) { ErrMsg.push(“Please enter first name”); }
if (isEmpty(forms.elements[“lastname”].value)) { ErrMsg.push(“Please enter last name”); }
if (isEmpty(forms.elements[“email”].value)) { ErrMsg.push(“Please enter e-mail address”); }
if (isEmpty(forms.elements[“employ”].value)) { ErrMsg.push(“Please enter assignment”); }

if (ErrMsg.length > 0) { alert(ErrMsg.join(‘n’)); flag = false; }
return flag;
}
function Success() {
var str = ‘Action was a successn’;
str +=’n’+isRBtnChecked(‘level’);
str +=’: ‘+document.MyForm.elements[‘firstname’].value;
str +=’ ‘+document.MyForm.elements[‘lastname’].value;
str +=’n’+document.MyForm.elements[’email’].value;
str +=’n’+document.MyForm.elements[’employ’].value;
alert(str);
}
</script>
</head>
<body>

<!– action in form below to be replaced by valid program address –>
<form name=”MyForm” id=”my_form” method=”post” action=”javascript:Success()” onsubmit=”return formCheck()”>

<table border=”0″>
<tr><td align=”right”>
Title:
</td><td>
<input type=”radio” name=”level” value=”Instructor” /> &nbsp; Instructor
<input type=”radio” name=”level” value=”Assistant” /> &nbsp; Assistant
<input type=”radio” name=”level” value=”Associate” /> &nbsp; Associate
<input type=”radio” name=”level” value=”Professor” /> &nbsp; Professor<br />
</td></tr>

<tr><td align=”right”>
Name:
</td><td>
First: <input type=”text” name=”firstname”>
Initial: <input type=”text” name=”middleinitial” size=”1″ maxlength=”1″>
Last: <input type=”text” name=”lastname”>
</td></tr>

<tr><td align=”right”>
E-mail Address:
</td><td>
<input type=”text” name=”email” size=”40″ value=”[email protected]”>
</td></tr>

<tr><td align=”right”>
Assignment:
</td><td>
<select name=”employ”>
<option value=””>- Select -</option>
<option value=”PT”>Part-time</option>
<option value=”AD”>Adjunct</option>
<option value=”FT”>Full-time</option>
</select>
</td></tr>
<tr><td>&nbsp;</td><td><button>Submit</button> <!– for testing purposes –></td></tr>
</table>

</form>
</body>
</html>
[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyJan 18.2009 — No DOCTYPE? What is the intent?
Copy linkTweet thisAlerts:
@MrNobodyJan 18.2009 — You also didn't indicate exactly what action you were trying to take which didn't work. But, instead of this:
[code=html]<button>Submit</button>[/code]
you'll need this to activate the validation process on form submit:
[code=html]<button type="submit">Submit</button>[/code]
Copy linkTweet thisAlerts:
@JMRKERauthorJan 18.2009 — Only intent was to make it work in FF and IE.

I did not realize the 'doctype' had so much influence.

I put in

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

and it works fine in both.

Thanks for the help.
Copy linkTweet thisAlerts:
@MrNobodyJan 18.2009 — I did not realize the 'doctype' had so much influence.

I put in

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

and it works fine in both.[/QUOTE]

Strange, because I put that same DOCTYPE and nothing happened until I changed the button as indicated in my second post above -- but then I wasn't testing directly in the IE browser, either. perhaps that was the problem. ?
Copy linkTweet thisAlerts:
@JMRKERauthorJan 18.2009 — You also didn't indicate exactly what action you were trying to take which didn't work. But, instead of this:
[code=html]<button>Submit</button>[/code]
you'll need this to activate the validation process on form submit:
[code=html]<button type="submit">Submit</button>[/code][/QUOTE]


I did not have an action to perform at the time of the testing so I just set-up an alert to see that information was being gathered and would be sent when I had finished the project. Submit button appears to work both ways at this time, probably because I had the 'onsubmit=' in the <form> tag.

If I'm doing something crazy here, let me know. Again original intent was to learn why I was not getting any error messages in MSIE, but that it still did not go to the action alert as the FF browser did, what I thought, correctly. The <doctype...> tag appears to fix problem in both browsers. I will check results on my Mac (Safari) at work early next week.

Again, thanks for the insights.

?
×

Success!

Help @JMRKER 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 4.29,
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,
)...