By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Help: Object Expected error

P: n/a
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>
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 19 Aug 2004 11:15:56 -0700, TAM <gr*******@canada.com> wrote:
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.

<script type="text/javascript">
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.