SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2002
    Location
    Madison, WI
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reg ex error, error setting string value

    Hi,

    Two small problems.

    1) Netscape 7 gives me this error in the script below:

    Error:unterminated character class
    num = num.replace(/[^0-9/-]/g, '');

    // allows numbers or dashes only, checks on each key up.
    function numbersDashesOnly(theform,thefield) {
    origField = eval("window.document."+theform+"."+thefield);
    num = eval("window.document."+theform+"."+thefield+".value");
    num = num.replace(/[^0-9/-]/g, '');
    origField.value = num;
    }

    2) For some odd reason, I cannot set the value of msg properly in this script. The scrip works, but the alert displays "0" instead of what msg should have been set to.

    function confirmAddCust() {
    // check zip, first and last names.
    var error = false;
    var msg = '';
    checkZip = window.document.Cust.ZipCode.value;
    checkfName = window.document.Cust.FirstName.value;
    checklName = window.document.Cust.LastName.value;
    if ((checkZip.length<5) && (checkZip.length!=0)) {
    error = true;
    msg = 'Zip code must be five digits.\nIf unknown enter 00000.\n'; }
    if ((checkfName.length<1) || (checklName.length<1)) {
    error = true;
    msg = msg & 'You must enter first and last names.'; }
    if (error) {
    alert(msg);
    return false; }
    else {
    window.document.Cust.submit();
    }
    }

    Please advise, thanks.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, character classes allow you to specify ranges, right? You have one range, 0-9. You also have (what netscape thinks) is the start of a second range /-

    It expects another character after the -

    I suspect the person that wrote this wanted to escape the dash so it wouldn't be interpreted as a range delimeter, but they used the wrong slash. The escaping slash is the backslash, not the foward slash.

    Here's the revised pattern

    /[^0-9\-]/g

    Additionally, instead of using eval in your function on these two lines
    Code:
    origField = eval("window.document."+theform+"."+thefield);
    num = eval("window.document."+theform+"."+thefield+".value");
    Use the collections made available instead
    Code:
    origField = document.forms[theform].elements[thefield];
    num = origField.value;
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot
    Join Date
    Oct 2002
    Location
    Madison, WI
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Well, character classes allow you to specify ranges, right? You have one range, 0-9. You also have (what netscape thinks) is the start of a second range /-

    It expects another character after the -

    I suspect the person that wrote this wanted to escape the dash so it wouldn't be interpreted as a range delimeter, but they used the wrong slash. The escaping slash is the backslash, not the foward slash.

    Here's the revised pattern

    /[^0-9\-]/g

    Thanks, that worked.

    Additionally, instead of using eval in your function on these two lines
    Code:
    origField = eval("window.document."+theform+"."+thefield);
    num = eval("window.document."+theform+"."+thefield+".value");
    Use the collections made available instead
    Code:
    origField = document.forms[theform].elements[thefield];
    num = origField.value;
    Is there anything wrong with using eval? Do you prefer collections because its easier to use, or less ugly?

    Any ideas on the second issue?

    Thanks!
    :::::::::::::::::::::::::::::::::::::::::::::::::::
    Versa Studio
    ExpressionEngine experts
    bas (at) versa studio dot com

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya - eval is a very costly operation, and can cause scope problems. And yes -- it's ugly.

    About your second problem, it's this line
    Code:
    msg = msg & 'You must enter first and last names.';
    I think & is the concatenation operator in VBScript, but it's certainly not in JavaScript. The ampersand in Javascript is a logical bitwise operator. The string concatenation operator is the plus-sign. (The plus-sign in JS is overloaded, as it handles arithmetic addition and string concatenation)
    Code:
    msg = msg + 'You must enter first and last names.';
    You can also use the compound concatenation/assignment operator
    Code:
    msg += 'You must enter first and last names.';
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Zealot
    Join Date
    Oct 2002
    Location
    Madison, WI
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem fixed. Thanks again.
    :::::::::::::::::::::::::::::::::::::::::::::::::::
    Versa Studio
    ExpressionEngine experts
    bas (at) versa studio dot com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •