HighDots Forums  

Re: javascript regular expression error

Macromedia Dreamweaver Macromedia Dreamweaver Discussions (macromedia.dreamweaver)


Discuss Re: javascript regular expression error in the Macromedia Dreamweaver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
T. Pastrana
 
Posts: n/a

Default Re: javascript regular expression error - 07-14-2003 , 09:03 PM






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
www.fourlevel.com




"Dwayne Epps" <dwayneepps (AT) centurytel (DOT) net> wrote

Quote:
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-





Reply With Quote
  #2  
Old   
Dwayne Epps
 
Posts: n/a

Default Re: javascript regular expression error - 07-15-2003 , 08:29 PM






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 (AT) NOSPAMMfourlevel (DOT) com> wrote

Quote:
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
www.fourlevel.com




"Dwayne Epps" <dwayneepps (AT) centurytel (DOT) net> wrote in message
news:bevat4$h2$1 (AT) forums (DOT) 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-







Reply With Quote
  #3  
Old   
T. Pastrana
 
Posts: n/a

Default Re: javascript regular expression error - 07-15-2003 , 10:05 PM



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
www.fourlevel.com




"Dwayne Epps" <dwayneepps (AT) centurytel (DOT) net> wrote

Quote:
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-




Reply With Quote
  #4  
Old   
T. Pastrana
 
Posts: n/a

Default Re: javascript regular expression error - 07-16-2003 , 10:12 AM



Good one... ;-)


--
..Trent Pastrana
www.fourlevel.com




"David Powers" <me (AT) thisisntmyaddress (DOT) com> wrote

Quote:
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
http://japan-interface.co.uk/webdesign/books.html
Save 10% on TopStyle CSS Editor
*******************************************





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.