Netscape getElementById Issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron M

    #1

    Netscape getElementById Issue



    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.lengt h; x++) {
    if (validNums.inde xOf(strString.c harAt(x)) != -1) {
    alertErr += "->This Field Cannot Contain Numbers\n";
    break;
    }
    }
    if (alertErr) {
    result = false;
    document.getEle mentById(strFie ld).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.getEle mentById(strFie ld) 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!
  • Lee

    #2
    Re: Netscape getElementById Issue

    Ron M said:[color=blue]
    >
    >
    >
    >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.lengt h; x++) {
    > if (validNums.inde xOf(strString.c harAt(x)) != -1) {
    > alertErr += "->This Field Cannot Contain Numbers\n";
    > break;
    > }
    > }
    > if (alertErr) {
    > result = false;
    > document.getEle mentById(strFie ld).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.getEle mentById(strFie ld) 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.[/color]

    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.sea rch(/\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="retur n CheckForNumbers (this.firstName )">
    <input name="firstName "><br>
    <input type="submit">
    </form>

    Comment

    • Ron M

      #3
      Re: Netscape getElementById Issue

      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!

      Comment

      • Dr John Stockton

        #4
        Re: Netscape getElementById Issue

        JRS: In article <421baeef_1@127 .0.0.1>, dated Tue, 22 Feb 2005
        16:15:11, seen in news:comp.lang. javascript, Ron M
        <anonymous@devd ex.com> posted :
        [color=blue]
        >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.[/color]

        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.val ue) 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.va lue) 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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...