473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Regular expression for carriage return/enter?


If the following line will force the user to input only a number
between 1 and 5, and will give an error to any keypress outside that
range:

onkeyup="if(event.srcElement.value.search(new RegExp('^[1-5]$')))
{alert('Please type in a number between 1 and
5');event.srcElement.value='';return false}"

.... how could I get the regular expression to accept 1 to 5 *or* the
Enter/Return keypress? I have tried ('^[1-5][\r]$') but that doesn't
work. Is there a syntax for saying "1 to 5 or Enter/Return"?

Thank you for any info you can give.

Steve
Apr 4 '08 #1
3 6796
Ah - figured it out: I'm barking up the wrong tree because the
"value" of the source element will never "equal" a Return character.
I needed to do: (new RegExp('^[1-5]$'))&&event.keyCode!=13)

Well, hopefully this post may be of use to anyone else looking for a
similar solution.

Steve
Apr 4 '08 #2
st*****@hotmail.com wrote:
If the following line will force the user to input only a number
between 1 and 5, and will give an error to any keypress outside that
range:

onkeyup="if(event.srcElement.value.search(new RegExp('^[1-5]$')))
{alert('Please type in a number between 1 and
5');event.srcElement.value='';return false}"
This is error-prone, incompatible, illegible, and inefficient. You were
looking for

maxlength="1"
onkeyup="if (!/[1-5]/.test(this.value))
{
window.alert('Please type in a number between 1 and 5');
this.value = '';
return false;
}"

instead. It is still awful. For example, I have just switched from that
focused control to the newsreader with Alt+Tab and got the error message.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Apr 4 '08 #3
stev...@hotmail.com wrote:
I'm barking up the wrong tree because the "value" of the source
element will never "equal" a Return character.
I needed to do: (new RegExp('^[1-5]$'))&&event.keyCode!=13)
Take this code Ad Unicem and it might not work anymore; the Enter-key
can give keycode 10 too.

10 = Carriage Return, as described in your subject line
13 = Newline, the most common keycode for the Enter-key AFAIK

... && event.keyCode != 10 && event.keyCode != 13

--
Bart
Apr 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
1
by: Jim Heavey | last post by:
In the Regular Expression, I can use \n for Line Feed, \t for a tab and \r for carriage return. Am I able to use thos same expressions in a "Replace method" of the string object? such as ...
2
by: David Cho | last post by:
I am using this expression \d+(,\s*\d+)* to allow only numbers and commas. But if there are carriage returns mixed, it is not validated. Is tehre a way to ignore all carriage returns? I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
5
by: Bosconian | last post by:
I have a textarea form field for inputting (or pasting) pairs of data. I need a regular expression pattern to validate each line for the following double quote number double quote comma...
6
by: rahatekarabhijeet | last post by:
I want regular expression which can extract the data between two symbols, e.g: <*skdjflsk (new line character) lksdjfklasjdfl (new line character) ksjdfkhasdkf (new line character) asdfjkhasdjk...
2
by: mehdi | last post by:
Hi, Consider the following string: <div>whatever text goes here, and maybe it contains a carriage return and/or line feed and it spans in multiple lines</div> How am I supposed to write an...
1
by: Blue | last post by:
This JS limits the input characters into the form. How do I modify it so that it also allows CARRIAGE RETURN and BACKSPACE (for making text correction)? Due to the template engine I am using, I...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.