javascript regular expression error

Ask a Question related to Macromedia Dreamweaver, Design and Development.

  1. #1

    Default Re: javascript regular expression error

    It works here but it's not fool proof. You didn't say why it isn't working
    for you. It could be that your putting in odd characters in the field or it
    could be the way your actually calling the function.

    A more accurate expression ( if only numbers are truly allowed ) would be
    this one.

    /^[0-9]*$/

    ....This will look for numbers only.

    Then in the if statement place an explanation mark before the condition
    stating that it is ( if not true ) instead of ( if it is true )...in other
    words ... if there are no numbers, or if there is anything else than a
    number even if some numbers are present.

    if (!field.value.match(/^[0-9]*$/)){

    --
    ..Trent Pastrana
    [url]www.fourlevel.com[/url]




    "Dwayne Epps" <dwayneepps@centurytel.net> wrote in message
    news:bevat4$h2$1@forums.macromedia.com...
    > I am trying to create a function that checks for only numbers within a
    text
    > field. Here is the function I'm using:
    >
    > function noLetters(field) {
    > if (field.value.match(/^[a-zA-Z]*$/)) {
    > alert('This field can only contain numbers.');
    > field.focus();
    > field.select();
    > return false;
    > }
    > return true;
    > }
    >
    > It is not working and I believe the error in the syntax is within the
    line:
    > if (field.value.match(/^[a-zA-Z]*$/))
    >
    > If anyone can identify the problem with the syntax it would be
    appreciated?
    > Thanks in advance.
    > -D-
    >
    >

    T. Pastrana Guest

  2. Similar Questions and Discussions

    1. Regular Expression
      Hi, I am writing a script that parses an html file (which has been retrieved as a scalar by LWP::UserAgent). The script looks for everything in...
    2. Regular expression help
      Hi, I'm pretty new to regular expressions. Before, I used to write long-winded and buggy segments of code with PHPs string functions to extract...
    3. Regular Expression - Need Help
      Hi, I have the following: <cfset input =" Origin: cohnok-530a (190.068.59.250) Type: ...
    4. JavaScript? Putting a variable in the midst of a Regular Expression.
      I was reading a great article by Kas Thomas, "Save Yourself Some Typing with 'With'" at PlanetPDF. I really don't care much for the term guru, I...
    5. Regular Expression Error
      I want to use headers to redirect the client if they didn't come from the correct page, but I have a problem with one page. If they leave a form...
  3. #2

    Default Re: javascript regular expression error

    Hi Trent,
    That worked great! I appreciate the help. If I could ask one more
    question. I know there is a way to include individual characters within a
    regular expression. For example, the following statement will find any
    character
    that doesn't match with all lowercase letters, uppercase letters and digits.
    How can I also add individual characters to the expression? I would like to
    include the whitespace character for a blank space and some other additional
    characters like the comma sign, etc. within the expression.

    if (!field.value.match(/^[a-zA-Z-0-9]*$/))

    Thanks again for your help!
    -D-




    "T. Pastrana" <tpastrana@NOSPAMMfourlevel.com> wrote in message
    news:bevjt9$c9d$1@forums.macromedia.com...
    > It works here but it's not fool proof. You didn't say why it isn't working
    > for you. It could be that your putting in odd characters in the field or
    it
    > could be the way your actually calling the function.
    >
    > A more accurate expression ( if only numbers are truly allowed ) would be
    > this one.
    >
    > /^[0-9]*$/
    >
    > ...This will look for numbers only.
    >
    > Then in the if statement place an explanation mark before the condition
    > stating that it is ( if not true ) instead of ( if it is true )...in other
    > words ... if there are no numbers, or if there is anything else than a
    > number even if some numbers are present.
    >
    > if (!field.value.match(/^[0-9]*$/)){
    >
    > --
    > ..Trent Pastrana
    > [url]www.fourlevel.com[/url]
    >
    >
    >
    >
    > "Dwayne Epps" <dwayneepps@centurytel.net> wrote in message
    > news:bevat4$h2$1@forums.macromedia.com...
    > > I am trying to create a function that checks for only numbers within a
    > text
    > > field. Here is the function I'm using:
    > >
    > > function noLetters(field) {
    > > if (field.value.match(/^[a-zA-Z]*$/)) {
    > > alert('This field can only contain numbers.');
    > > field.focus();
    > > field.select();
    > > return false;
    > > }
    > > return true;
    > > }
    > >
    > > It is not working and I believe the error in the syntax is within the
    > line:
    > > if (field.value.match(/^[a-zA-Z]*$/))
    > >
    > > If anyone can identify the problem with the syntax it would be
    > appreciated?
    > > Thanks in advance.
    > > -D-
    > >
    > >
    >
    >

    Dwayne Epps Guest

  4. #3

    Default Re: javascript regular expression error

    Dwayne,

    Well, you can add the characters you want to the character set you have
    defined in the expression. The current set contains numbers only. Just add
    the characters you wish to allow at the end.

    For example.. what was previously this ... [0-9]

    now become this... [0-9\s,.]

    The \s allows for spaces.
    The , allows for commas.
    The . allows for peroids.
    etc...

    So if you wanted to add say a colon to the equation above just add it in
    there at the end.

    and it would look like this... [0-9\s,.:]


    --
    ..Trent Pastrana
    [url]www.fourlevel.com[/url]




    "Dwayne Epps" <dwayneepps@centurytel.net> wrote in message
    news:bf26ju$mse$1@forums.macromedia.com...
    > Hi Trent,
    > That worked great! I appreciate the help. If I could ask one more
    > question. I know there is a way to include individual characters within a
    > regular expression. For example, the following statement will find any
    > character
    > that doesn't match with all lowercase letters, uppercase letters and
    digits.
    > How can I also add individual characters to the expression? I would like
    to
    > include the whitespace character for a blank space and some other
    additional
    > characters like the comma sign, etc. within the expression.
    >
    > if (!field.value.match(/^[a-zA-Z-0-9]*$/))
    >
    > Thanks again for your help!
    > -D-


    T. Pastrana Guest

  5. #4

    Default Re: javascript regular expression error

    Good one... ;-)


    --
    ..Trent Pastrana
    [url]www.fourlevel.com[/url]




    "David Powers" <me@thisisntmyaddress.com> wrote in message
    news:bf3876$8ov$1@forums.macromedia.com...
    > T. Pastrana wrote:
    > ::
    > :: /^[0-9]*$/
    > ::
    > :: ...This will look for numbers only.
    >
    > It will also match a blank field. The * means match zero or more. To
    ensure
    > at least one number is included /^[0-9]+$/
    >
    >
    > --
    > David Powers
    > *******************************************
    > No-nonsense reviews of computer books
    > [url]http://japan-interface.co.uk/webdesign/books.html[/url]
    > Save 10% on TopStyle CSS Editor
    > *******************************************
    >
    >

    T. Pastrana Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139