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

Netscape getElementById Issue

P: n/a


I am sure this is a simple issue, but being a beginner I am struggling.
In the following js function I am attempting to make sure no numbers are
within "name" controls. I want to pass the text value of the HTML
control and the control name into the function:
function CheckForNumbers (strString, strField) {
var result = true;
var validNums = "0123456789";
var alertHeader = "The following error(s) occured:\n\n";
var alertErr = "";
for (x=0; x < strString.length; x++) {
if (validNums.indexOf(strString.charAt(x)) != -1) {
alertErr += "->This Field Cannot Contain Numbers\n";
break;
}
}
if (alertErr) {
result = false;
document.getElementById(strField).focus();
alert (alertHeader + alertErr);
}

return result;
}

I don't know if this is the smartest way to do this, just the way I
thought of. The bottom line is I would like the code to check the value
to see if there is a number, and set focus back to the offending control
for the user to correct the problem. This works without a problem in
Internet Explorer, version 6 (latest Service packs, hot fixes, etc), but
in Netscape, version 7.2, I am getting the following error in the
JavaScript console:
Error: document.getElementById(strField) has no properties
Source File: http://127.0.0.1:8100/TestJscript.jsp
Line: 104

Sorry this is so long, but I wanted to be as complete as possible and I
appreciate any help.
Ron
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Lee
Ron M said:

I am sure this is a simple issue, but being a beginner I am struggling.
In the following js function I am attempting to make sure no numbers are
within "name" controls. I want to pass the text value of the HTML
control and the control name into the function:
function CheckForNumbers (strString, strField) {
var result = true;
var validNums = "0123456789";
var alertHeader = "The following error(s) occured:\n\n";
var alertErr = "";
for (x=0; x < strString.length; x++) {
if (validNums.indexOf(strString.charAt(x)) != -1) {
alertErr += "->This Field Cannot Contain Numbers\n";
break;
}
}
if (alertErr) {
result = false;
document.getElementById(strField).focus();
alert (alertHeader + alertErr);
}

return result;
}

I don't know if this is the smartest way to do this, just the way I
thought of. The bottom line is I would like the code to check the value
to see if there is a number, and set focus back to the offending control
for the user to correct the problem. This works without a problem in
Internet Explorer, version 6 (latest Service packs, hot fixes, etc), but
in Netscape, version 7.2, I am getting the following error in the
JavaScript console:
Error: document.getElementById(strField) has no properties
Source File: http://127.0.0.1:8100/TestJscript.jsp
Line: 104

Sorry this is so long, but I wanted to be as complete as possible and I
appreciate any help.


It works in IE because IE considers the "name" field to be sorta
kinda the same as the "id" field, whereas Netscape was written by
reasonable people who insist that you actually pass an id value to
getElementById().

It's almost always better to pass a reference to an control than
to pass the name. That way you avoid having to look it up later,
and the value doesn't have to be passed separately:
function CheckForNumbers (field) {
var result=true;
if (-1 != field.value.search(/\d/) ) {
// The search for a digit in the value was sucessful
result = false;
alert ("This Field Cannot Contain Numbers");
field.focus();
}
return result;
}

<form onsubmit="return CheckForNumbers(this.firstName)">
<input name="firstName"><br>
<input type="submit">
</form>

Jul 23 '05 #2

P: n/a
I will try that, thank you for the quick response. Ron

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3

P: n/a
JRS: In article <42********@127.0.0.1>, dated Tue, 22 Feb 2005
16:15:11, seen in news:comp.lang.javascript, Ron M
<an*******@devdex.com> posted :
I am sure this is a simple issue, but being a beginner I am struggling.
In the following js function I am attempting to make sure no numbers are
within "name" controls.


But what, for you, is a "number"? There are two numbers in the name of
one who had a narrow escape almost 400 years ago, James I & VI. Do you
perhaps mean "decimal digits"? If you do, then say so.
You can use var Bad = /\d/.test(field.value) for that.

However, what you ask for is probably not what you want; it will allow
an empty string, or one containing any characters other than the ten
digits, probably including whatever character \uABCD may be.

Assuming that "name" is something like a personal or geographical name,
and that the working language is a dialect of English, there is a finite
set of characters that one needs to allow in name fields ;
a-z A-Z space ' ! - at least.

So var OK = /^[a-z '!-]+$/i.test(field.value) though
you may (wrongly) consider just one character to be too short.

For real names of Continental origin, various accented characters should
be allowed.

The test should be done, however, after trimming the field of leading or
trailing spaces - newsgroup FAQ 4.16.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.