![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
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- |
#2
| |||
| |||
|
|
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- |
#3
| |||
| |||
|
|
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- |
#4
| |||
| |||
|
|
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 ******************************************* |
![]() |
| Thread Tools | |
| Display Modes | |
| |