PDA

View Full Version : Javascript not working in ie8



hemi519
12-13-2010, 06:28 PM
Hi All,

the below code is not working in ie8,

<script type="text/javascript">
<!--

function validate_form ( )
{

valid = true;

var pattern=/([A-Za-z]{2,20}$)+/


else if (!pattern.test(document.newform.name.value) )
{
alert ( "Please enter the missing value" );
document.newform.name.focus();
valid = false;
}

valid=true;
}
..>
</script>

<form action="valid.php" method="post" name=newform onsubmit="return validate_form ( );">
<table>
<tr><td>Name</td><td><input type=text name="name" ></td></tr>
</table>


I am able to get the validation in chrome and firefox. But i need it in ie8 also, can anyone help me in this

bluewalrus
12-13-2010, 07:42 PM
IE might not like the name of your form. Try putting the name in quotes

so


name=newform

becomes


name="newform"

hemi519
12-13-2010, 08:02 PM
thanks for that, iam having another doubt, in the form tag, action=someotherpage.php(not the page where i written this html and javascript page ) can i do validations in that also. iam unable to do so, it is showing for the first filed and automatically redirecting to someotherpage.php. how to stop this.

bluewalrus
12-13-2010, 08:57 PM
I'm not exactly clear what you mean by validation ((x)html valid or data input valid). The action attribute is where the form will go to be processed.

hemi519
12-13-2010, 09:10 PM
if my page is valid.html and if my action=reg.html will that work with the perfect validation. In my case after the first field validation it is directly redirecting to reg.html, it is not asking for the second field. how can i do that

bluewalrus
12-13-2010, 09:30 PM
Please post your full code or a link.

hemi519
12-13-2010, 09:38 PM
-------valid.html--------------

in action tag <form action=validd.html>

<html>
<head>
<script type="text/javascript">

<!--
function validate_form ( )
{

valid = true;

var pattern=/([A-Za-z]{2,20}$)+/;


if (!pattern.test(document.newform.name.value) )
{
alert ( "Please enter the missing value" );
document.new.nameform.focus();
valid = false;
}


else if (!pattern.test(document.newform.name1.value) )
{
alert ( "Please enter the missingc2 value" );
document.newform.name1.focus();
valid = false;
}

alert("DDD");

return valid;
}
-->
</script>
</head>
<body>
<form action="validd.html" method="post" name="newform" onsubmit="return validate_form ( );">
<table>
<tr><td>Name</td><td><input type=text name="name" ></td></tr>
<tr><td>Name</td><td><input type=text name="name1" ></td></tr>
<tr><td> <input type=submit > </td></tr>
</table>
</form>
</body>
</html>

bluewalrus
12-13-2010, 09:49 PM
You don't need the form at all for this, I think. The part I think that was throwing you off was the else.



<head>
<script type="text/javascript">
<!--
function validate_form ( ){
valid = true;
var pattern=/([A-Za-z]{2,20}$)+/;
if (!pattern.test(document.newform.name.value) ) {
alert ( "Please enter the missing value" );
document.new.nameform.focus();
valid = false;
}
if (!pattern.test(document.newform.name1.value) ) {
alert ( "Please enter the missingc2 value" );
document.newform.name1.focus();
valid = false;
}
alert("DDD");
return valid;
}
-->
</script>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" ></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name1" ></td>
</tr>
<tr>
<td><input type="submit" onClick="return validate_form ( );" > </td>
</tr>
</table>
</body>

hemi519
12-13-2010, 09:53 PM
u removed my form tag, i want that form tag with action to different page

bluewalrus
12-13-2010, 10:07 PM
Oh, I thought you were going to the same page. You can just through it back in there than. If the user has JS turned off though this validation won't be performed usually this is passed to a server side page were an additional validation is performed if the data is suppose to be secure.


<html>
<head>
<script type="text/javascript">
<!--
function validate_form ( ){
valid = true;
var pattern=/([A-Za-z]{2,20}$)+/;
if (!pattern.test(document.newform.name.value) ) {
alert ( "Please enter the missing value" );
document.new.nameform.focus();
valid = false;
}
if (!pattern.test(document.newform.name1.value) ) {
alert ( "Please enter the missingc2 value" );
document.newform.name1.focus();
valid = false;
}
alert("DDD");
return valid;
}
-->
</script>
</head>
<body>
<form action="validd.html" method="post" name="newform" onsubmit="return validate_form ( );">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" ></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name1" ></td>
</tr>
<tr>
<td><input type="submit"> </td>
</tr>
</table>
</form>
</body>
</html>

hemi519
12-14-2010, 07:54 AM
thanks for that, the error was in javascript in the line
document.new.nameform.focus();

this has to be returned in this way
document.newform.name.focus();

sometimes it happens we dont see small mistakes