i am using the required attribute and that works well for FF and chrome but not IE. so i am having to write some js. however, i want the js only to run if the browser is IE. how can i achieve this? and is the below code the best way to check for field requirement?
could it better written better than loads of conditional statements?
function validateForm() {
var name = document.forms["ContactUsForm"]["txtname"].value;
var profession = document.forms["ContactUsForm"]["profession"].value;
var email = document.forms["ContactUsForm"]["email"].value;
var confirmemail = document.forms["ContactUsForm"]["confirmemail"].value;
var message = document.forms["ContactUsForm"]["message"].value;
if (name == null || name == "") {
alert("Please fill in Name field");
return false;
}
else {
return true;
}
if (profession == null || profession == "") {
alert("Please fill in Profession field");
return false;
}
else {
return true;
}
if (email == null || email == "") {
alert("Please fill in Email field");
return false;
}
else {
return true;
}
if (confirmemail == null || confirmemail == "") {
alert("Please fill in Confirm Email field");
return false;
}
else {
return true;
}
if (message == null || message == "") {
alert("Please fill in Message field");
return false;
}
else {
return true;
}
}
function ValidateHTML5attribute() {
var name = document.forms["form1"]["myname"];
if (!Modernizr.input.required) {
if (name == null || name == "") {
alert("Please fill in Name field");
return false;
}
}
}
dm007
Member
47 Points
106 Posts
run js code only for IE
Jan 04, 2013 09:24 PM|LINK
i am using the required attribute and that works well for FF and chrome but not IE. so i am having to write some js. however, i want the js only to run if the browser is IE. how can i achieve this? and is the below code the best way to check for field requirement? could it better written better than loads of conditional statements?
function validateForm() { var name = document.forms["ContactUsForm"]["txtname"].value; var profession = document.forms["ContactUsForm"]["profession"].value; var email = document.forms["ContactUsForm"]["email"].value; var confirmemail = document.forms["ContactUsForm"]["confirmemail"].value; var message = document.forms["ContactUsForm"]["message"].value; if (name == null || name == "") { alert("Please fill in Name field"); return false; } else { return true; } if (profession == null || profession == "") { alert("Please fill in Profession field"); return false; } else { return true; } if (email == null || email == "") { alert("Please fill in Email field"); return false; } else { return true; } if (confirmemail == null || confirmemail == "") { alert("Please fill in Confirm Email field"); return false; } else { return true; } if (message == null || message == "") { alert("Please fill in Message field"); return false; } else { return true; } }dm007
Member
47 Points
106 Posts
Re: run js code only for IE
Jan 05, 2013 12:06 AM|LINK
(1) code modified - works ok. just have the one else statement right at the end now.
(2) i found that i could use this to get js to run only in IE:
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: run js code only for IE
Jan 05, 2013 12:43 AM|LINK
Hi,
Modernizer JavaScript library comes handy in this situation - http://modernizr.com/
With that, you can check the required attribute is supported by the browser or else you can provide the fall back method for the same!
Hope it helps u...
Roopesh Reddy C
Roopesh's Space
dm007
Member
47 Points
106 Posts
Re: run js code only for IE
Jan 05, 2013 06:21 PM|LINK
hey,
isn't there a way to just check the browser than checking whether if i am using html5 attributes?
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: run js code only for IE
Jan 06, 2013 03:22 AM|LINK
Hi,
It's recommended to test the features than browsers! Anyway check this link - http://stackoverflow.com/questions/2400935/browser-detection-in-javascript
Hope it helps u...
Roopesh Reddy C
Roopesh's Space
dm007
Member
47 Points
106 Posts
Re: run js code only for IE
Jan 06, 2013 07:22 PM|LINK
hey - i created this simple test. but i was expecting to see alert in IE but nothing happens!
<body> <form id="form1" runat="server"> <div> <div> Name: <input type="text" name="myname" required /> <br /> <asp:Button ID="btnSubmit" Text="Submit" OnClientClick="ValidateHTML5attribute()" runat="server" /> </div> </div> <script src="js/modernizr-1.5.min.js" type="text/javascript"></script> <script src="js/j1.js" type="text/javascript"></script> </form> </body>JS:
function ValidateHTML5attribute() { var name = document.forms["form1"]["myname"]; if (!Modernizr.input.required) { if (name == null || name == "") { alert("Please fill in Name field"); return false; } } }dm007
Member
47 Points
106 Posts
Re: run js code only for IE
Jan 06, 2013 07:43 PM|LINK
ah - i needed to run the modernizr script in head! cool - works now.