473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(eve nt.srcElement.v alue.search(new RegExp('^[1-5]$')))
{alert('Please type in a number between 1 and
5');event.srcEl ement.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 6834
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.key Code!=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(eve nt.srcElement.v alue.search(new RegExp('^[1-5]$')))
{alert('Please type in a number between 1 and
5');event.srcEl ement.value=''; return false}"
This is error-prone, incompatible, illegible, and inefficient. You were
looking for

maxlength="1"
onkeyup="if (!/[1-5]/.test(this.valu e))
{
window.alert('P lease 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.key Code!=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
3037
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 other words, as the regular expression (....)($) matches the "4567" in the string "1234567", how would I create a similar regular expression that only matches the "45" in the same string. The same regular expression would match "32" in the string...
1
2329
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 NewDescrp = NewDescrp.Replace('\n', ' '); //replace Line feed with space NewDescrp = NewDescrp.Replace('\t', ' '); //replace vertical tab with space NewDescrp = NewDescrp.Replace('\r', ' '); //replace Carriage return with a space
2
6156
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 am a RE newbie. Thanks.
9
3360
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 an app call The Regulator, which makes it pretty easy to build and test regular expressions. As a warning, I'm real weak with regular expressions. Let's say my regular expression is:
5
2265
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 double quote alpha string
6
4168
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 skfhsa (new line character) sdkfjklsd *> The no of lines may not be fix, those may vary. But the whole string will
2
1258
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 expression that returns the div's content?
1
6263
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 cannot use IF/ELSE statement. ============================== <form> <textarea name="event_description" ONKEYPRESS="if (document.layers)
14
4993
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 text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.