what does this mean? !/^\d+$/.test(el.value)

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

    #1

    what does this mean? !/^\d+$/.test(el.value)

    Hi Folks

    I have inhereted a script that I understand reasonably well, I just do not
    understand

    !/^\d+$/.test(el.value)

    what the hell does that mean?

    Below is the script (there are really three and they validate a four items
    on a form (min and max rate, (should be between 0 and 99999) and min and max
    rooms (should be between 0 and 99)).

    TIA

    - Nicolaas


    function valid(form) {// thoroughly validates form
    var e = 0
    var M = ''
    //M = BasicCheck (form)

    // start checks

    var field = form.max_rooms;
    var vm = parseInt(field. value, 10);
    if (!vm && vm !=0) {
    M = M + " You have to indicate the maximum number of rooms - enter 99 to
    ensure all properties are included.";
    field.focus();
    field.select();
    e = e + 1
    } else if (vm >= 0 && vm < 100){
    //do nothing
    } else {
    M = M + " You have to indicate the maximum number of rooms - enter 99 to
    ensure all properties are included. You entered " + vm + ".";
    field.focus();
    field.select();
    e = e + 1;
    }

    var field = form.min_rooms;
    var vi = parseInt(field. value, 10);
    if (!vi && vi !=0) {
    M = M + " You have to indicate the minimum number of rooms - enter 0 to
    ensure ensure all properties are included.";
    field.focus();
    field.select();
    e = e + 1
    } else if (vi >= 0 && vi < 100){
    //do nothing
    } else {
    M = M + " You have to indicate the minimum number of rooms - enter 0 to
    ensure all properties are included. You entered " + vi + ".";
    field.focus();
    field.select();
    e = e + 1;
    }

    if (vm <= vi){
    M = M + " the maximum number of rooms should be greater than the minimum
    number of rooms. "
    field.focus ();
    field.select ();
    e = e + 1
    }

    var field = form.max_rate;
    var vm = parseInt(field. value, 10);
    if (!vm && vm !=0) {
    M = M + " You have to indicate the maximum tariff in New Zealand dollars
    (e.g. 300) - enter 99999 to ensure that all properties are included.";
    field.focus();
    field.select();
    e = e + 1;
    } else if (vm >= 0 && vm >= 99999) {
    // do nothing
    } else {
    M = M + " You have to indicate the maximum tariff in New Zealand dollars
    (e.g. 300) - enter 99999 to ensure that all properties are included. Yyou
    entered " + vm + ".";
    field.focus();
    field.select();
    e = e + 1;
    }
    var field = form.min_rate;
    var vi = parseInt(field. value, 10);
    if (!vi && vi !=0) {
    M = M + " You have to indicate the maximum tariff in New Zealand dollars
    (e.g. 300) - enter 99999 to ensure that all properties are included.";
    field.focus();
    field.select();
    e = e + 1;
    } else if (vi >= 0 && vi >= 99999) {
    // do nothing
    } else {
    M = M + " You have to indicate the minimum tariff in New Zealand dollars
    (e.g. 150) - enter 0 to ensure that all properties are included. You entered
    " + vi + ".";
    field.focus();
    field.select();
    e = e + 1;
    }

    if (e < 1) { //no errors occured
    // do nothing
    } else {
    document.getEle mentById('err') .innerHTML = M;
    }
    }



    function BasicCheck(form ){ //validates booking form
    var a=[], msg=[], index=1, e = 0 ;

    // re-test the fields
    a[a.length]=check(form.ele ments["min_rate"], "minimum rate", "empty",
    "5num");
    a[a.length]=check(form.ele ments[1], "maximum rate", "empty", "5num");
    a[a.length]=check(form.ele ments[2], "minimum number of rooms", "empty",
    "2num");
    a[a.length]=check(form.ele ments[3], "maximum number of rooms", "empty",
    "2num");

    //analyse the tests
    for(var ii=0; ii<a.length; ii++)
    if(a[ii].error){
    msg[msg.length]=a[ii].error;
    }
    //alert the message, if any
    if(msg.length) {
    e = e + 1
    document.getEle mentById('error msg').innerHTML = (msg.join("\n") );
    //handle form's submission
    return !msg.length;
    }
    }


    function check(el, field) {
    //checks a field, e.g. check(form.elem ents[0], "fieldname" , "empty",
    "2num"); el = element number
    //controller
    var result={error:" "};
    for(var ii=2; ii<arguments.le ngth; ii++)
    if((result=sing leCheck(el, field, arguments[ii])).error)
    break;
    return result;
    //single check
    function singleCheck(el, field, type) {
    var result={error:" "};
    switch(type) {
    case "empty" :
    if(/^\s*$/.test(el.value) ) {
    result.error= field+": should be completed.<BR>" ;
    }
    break;
    case "2num" :
    if(!/^\d+$/.test(el.value) ) {
    result.error=fi eld+": should contain up to two digits only (e.g.
    01).<BR>";
    } else {
    el.value=padLef t(el.value.repl ace(/^\d+(\d\d)$/,"$1"), "0", 2);
    }
    break;
    case "5num" :
    if(!/^\d+$/.test(el.value) ) {
    result.error=fi eld+": should contain up to five digits only (e.g.
    01).<BR>";
    } else {
    el.value=padLef t(el.value.repl ace(/^\d+(\d\d)$/,"$1"), "0", 5);
    }
    break;
    }
    if(el.style){
    el.style.color = result.error ? "#c00" : "";
    }
    return result;
    }
    }


    function padLeft(str, pad, count) { //turns a number into a string
    while(str.lengt h<count)
    str=pad+str;
    return str;
    }




  • ZER0

    #2
    Re: what does this mean? !/^\d+$/.test(el.value)

    On Mon, 17 Jan 2005 20:29:26 +1300, WindAndWaves wrote:
    [color=blue]
    > I have inhereted a script that I understand reasonably well, I just do not
    > understand
    >
    > !/^\d+$/.test(el.value)[/color]
    [color=blue]
    > what the hell does that mean?[/color]

    This is a Regular Expression. In that case, check if the property value of
    "el" contains only number chars (\d+) , from begin (^) to end ($) of the
    string.
    As you see, the result is "true" only for positive integer.

    P.S.
    Sorry for my english ^_^;

    --
    ZER0

    ~ The Tangent Universe collapsed 5923 days, 1 hours, 57 minutes and 13 seconds ago.

    Comment

    • WindAndWaves

      #3
      Re: what does this mean? !/^\d+$/.test(el.value)


      "ZER0" <zer0.shock@lib ero.it> wrote in message
      news:pva007r8nt s7$.dlg@ID-171124.news.ind ividual.net...[color=blue]
      > On Mon, 17 Jan 2005 20:29:26 +1300, WindAndWaves wrote:
      >[color=green]
      > > I have inhereted a script that I understand reasonably well, I just do[/color][/color]
      not[color=blue][color=green]
      > > understand
      > >
      > > !/^\d+$/.test(el.value)[/color]
      >[color=green]
      > > what the hell does that mean?[/color]
      >
      > This is a Regular Expression. In that case, check if the property value of
      > "el" contains only number chars (\d+) , from begin (^) to end ($) of the
      > string.
      > As you see, the result is "true" only for positive integer.
      >
      > P.S.
      > Sorry for my english ^_^;
      >
      > --
      > ZER0
      >
      > ~ The Tangent Universe collapsed 5923 days, 1 hours, 57 minutes and 13[/color]
      seconds ago.[color=blue]
      >[/color]

      Thank you - from zero to hero in a matter of seconds.


      Comment

      • Evertjan.

        #4
        Re: what does this mean? !/^\d+$/.test(el.value)

        ZER0 wrote on 17 jan 2005 in comp.lang.javas cript:
        [color=blue]
        > On Mon, 17 Jan 2005 20:29:26 +1300, WindAndWaves wrote:
        >[color=green]
        >> I have inhereted a script that I understand reasonably well, I just
        >> do not understand
        >>
        >> !/^\d+$/.test(el.value)[/color]
        >[color=green]
        >> what the hell does that mean?[/color]
        >
        > This is a Regular Expression. In that case, check if the property
        > value of "el" contains only number chars (\d+) , from begin (^) to end
        > ($) of the string.
        > As you see, the result is "true" only for positive integer.[/color]



        result = !/^\d+$/.test(el.value)

        is testing for "not all numbers" in the string.

        The same result and simpler is testing for "minimal one not-a-number":

        result = /\D/.test(el.value)


        --
        Evertjan.
        The Netherlands.
        (Replace all crosses with dots in my emailaddress)

        Comment

        • Dr John Stockton

          #5
          what does this mean? !/^\d+$/.test(el.value)

          JRS: In article <_SJGd.8606$mo2 .642114@news.xt ra.co.nz>, dated Mon, 17
          Jan 2005 20:29:26, seen in news:comp.lang. javascript, WindAndWaves
          <access@ngaru.c om> posted :[color=blue]
          >
          >I have inhereted a script that I understand reasonably well, I just do not
          >understand
          >
          >!/^\d+$/.test(el.value)
          >
          >what the hell does that mean?[/color]

          It means : not one-or-more decimal digits and nothing else in el.value.

          If number zero is not allowed, [1-9]\d* would be better than \d+ .
          If large numbers like 1000000000 are not allowed, use \d{m,n} to specify
          the allowed number of digits.

          The inherited code is wastefully repetitive; factor out common parts.

          Don't allow your posting system to wrap code lines; see FAQ. Code
          should be executable as transmitted.

          See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

          Rather than giving long-winded error messages, repetitively coded, it is
          better to make sure that the HTML clearly indicates the requirements for
          each field of the form - plain numbers, 0 <= minrate <= maxrate, 0 <=
          minrooms <= maxrooms . It is then at most necessary to indicate which
          field is in error, and perhaps whether the complaint is of format or
          value.

          However, with ranges of 0-99, 0-99999 there is no need to check values
          (except that max>=min) since by allowing only 1-2 or 1-5 digits the
          range is already enforced.

          Scrap the code, and rewrite it from the beginning.

          --
          © 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

          • Dr John Stockton

            #6
            what does this mean? !/^\d+$/.test(el.value)

            JRS: In article <Xns95E1B5CD846 5Deejj99@194.10 9.133.29>, dated Mon, 17
            Jan 2005 16:52:14, seen in news:comp.lang. javascript, Evertjan.
            <exjxw.hannivoo rt@interxnl.net > posted :[color=blue]
            >ZER0 wrote on 17 jan 2005 in comp.lang.javas cript:
            >[color=green]
            >> On Mon, 17 Jan 2005 20:29:26 +1300, WindAndWaves wrote:
            >>[color=darkred]
            >>> I have inhereted a script that I understand reasonably well, I just
            >>> do not understand
            >>>
            >>> !/^\d+$/.test(el.value)[/color]
            >>[color=darkred]
            >>> what the hell does that mean?[/color]
            >>
            >> This is a Regular Expression. In that case, check if the property
            >> value of "el" contains only number chars (\d+) , from begin (^) to end
            >> ($) of the string.
            >> As you see, the result is "true" only for positive integer.[/color]
            >
            >
            >
            >result = !/^\d+$/.test(el.value)
            >
            >is testing for "not all numbers" in the string.[/color]

            That means not (all digits), rather than (not all) digits.

            The code requires at least one digit.
            [color=blue]
            >The same result and simpler is testing for "minimal one not-a-number":
            >
            >result = /\D/.test(el.value)[/color]

            That only requires no non-digits; it does not require any digits, and
            will accept "". Then parseInt will give NaN, and + will give 0 ; the OP
            has a choice to make.

            --
            © 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

            • Evertjan.

              #7
              Re: what does this mean? !/^\d+$/.test(el.value)

              Dr John Stockton wrote on 17 jan 2005 in comp.lang.javas cript:
              [color=blue][color=green]
              >>result = !/^\d+$/.test(el.value)
              >>
              >>is testing for "not all numbers" in the string.[/color]
              >
              > That means not (all digits), rather than (not all) digits.
              >
              > The code requires at least one digit.
              >[color=green]
              >>The same result and simpler is testing for "minimal one not-a-number":
              >>
              >>result = /\D/.test(el.value)[/color]
              >
              > That only requires no non-digits; it does not require any digits, and
              > will accept "". Then parseInt will give NaN, and + will give 0 ; the
              > OP has a choice to make.[/color]


              True.

              The OC [original coder] didn't use:

              result = !/^\d*$/.test(el.value)

              and that is equivalent to:

              result = /\D/.test(el.value)

              --
              Evertjan.
              The Netherlands.
              (Replace all crosses with dots in my emailaddress)

              Comment

              Working...