Help: Object Expected error

T

TAM

Hi,

I have a simple JavaScript code that ensures that all the form fields
are filled and there is also a function that checks if the email is a
valid address. For some reason IE is giving "Object Expected" error on
the alert statement. The same code doesn't give an error in NN or
Opera or Mozilla or FireFox.

Can someone give me a clue as what could be wrong here?

Thanks,

joe

<script language = "JavaScript">
function validate(){
if ((document.SolutionEval.pwd.value ==
"")||(document.SolutionEval.pwd.value == null)){
alert("Please enter correct password")
document.SolutionEval.pwd.focus()
return false;
}
if (document.SolutionEval.name.value == ""){
alert("Please enter your name")
document.SolutionEval.name.focus()
return false;
}
if (document.SolutionEval.company.value == ""){
alert("Please enter your company name")
document.SolutionEval.company.focus()
return false;
}
if (document.SolutionEval.phone.value == ""){
alert("Please enter your phone number")
document.SolutionEval.phone.focus()
return false;
}
if ((document.SolutionEval.email.value ==
"")||(document.SolutionEval.email.value == null)){
alert("Please enter your email address")
document.SolutionEval.email.focus()
return false;
}
/* check if the email address is valid */
if (echeck(document.SolutionEval.email.value)==false){
document.SolutionEval.email.value=""
document.SolutionEval.email.focus()
return false;
}
return true;
}

function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 ||
str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 ||
str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot ||
str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID") << This where IE gives error "Object
Expected"
return false
}

return true
}
</script>
 
M

Michael Winter

I have a simple JavaScript code that ensures that all the form fields
are filled and there is also a function that checks if the email is a
valid address. For some reason IE is giving "Object Expected" error on
the alert statement. The same code doesn't give an error in NN or
Opera or Mozilla or FireFox.

Can someone give me a clue as what could be wrong here?

I can't see any problems with your code per se, but there are some
improvements to be made. The e-mail address validation, for instance, can
be greatly simplified with a regular expression.
<script language = "JavaScript">

Valid HTML requires the type attribute. The language attribute is
deprecated and shouldn't be used any more.

function validate(){

The first thing you could do here is save a reference to the form,
specifically, it's elements collection. It will save you having to type
out 'document.SolutionEval.' every time:

var elem = document.forms['SolutionEval'].elements;

To access the password control below, you would write:

elem['pwd'].value
if ((document.SolutionEval.pwd.value ==
"")||(document.SolutionEval.pwd.value == null)){

The value property will *never* be null. Testing for that condition is
pointless. Also, you can evaluate a string as a boolean (true/false). If
the string is empty (length zero), the expression will be false (see
below).
alert("Please enter correct password")
document.SolutionEval.pwd.focus()
return false;

Based on all of that, you could write the above as:

/* The ! (logical NOT) operator evaluates an expression
* as a boolean, then reverses the result.
*
* if(elem['pwd'].value) {
* // pwd contains at least one character
* } else {
* // pwd is empty
* }
*
* Add ! to the if expression (as I do below), and the first
* block will be executed if pwd is empty.
*/
if(!elem['pwd'].value) {
alert('Please enter a password.');
elem['pwd'].focus();
return false;
}

You can apply the same pattern to all of the other checks.

[snip]

As I said earlier, you can check an e-mail address with a regular
expression. The one below, taken from Dr Stockton's validation page is
greatly simplified, and will allow some invalid addresses, but it does
ensure basic form.

/^.+@.+\..+$/

You could change the last block in your validate function to:

if(!/^.+@.+\..+$/.test(elem['email'].value)) {
alert('Please enter a valid address.');
elem['email'].focus();
return false;
}

and delete echeck().

Hope that helps,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top