I'm trying to set up client side validation for a textarea form element to
ensure that the data entered does not exceed 200 characters. I'm using the
following code but it doesn't seem to be working correctly:
if( this.value != '' ) {
if( !( RegExp( '^[.]{0,200}$' ).test( this.value ))) {
alert( 'Information provided must not be more than 200 characters.' );
this.focus();
}
}
I'm attaching the code to the OnBlur event. The test is passed if there is
no data in the text area. It fails if there is data, regardless of what
that data consists of. What's going on? I thought the dot matched any
character? Also, I've tried removing the beginning and ending ^$, but if I
do that then the test always seems to pass, even if the text is greater than
200 characters in length.
What am I doing wrong?
thnx,
Christoph 6 1386
Christoph wrote: I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be working correctly:
if( this.value != '' ) {
if (this.value.length > 200) {
alert(...);
}
}
I don't think you need a regular expression to test the number of
characters.
--
Martin Honnen http://JavaScript.FAQTs.com/
Christoph wrote on 17 apr 2006 in comp.lang.javascript : if( this.value != '' ) { if( !( RegExp( '^[.]{0,200}$' ).test( this.value ))) { alert( 'Information provided must not be more than 200 characters.' ); this.focus(); } }
simplified:
if (this.value.length>200) {
alert( 'Information provided must not be more than 200 characters.' );
this.focus();
}
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Martin Honnen wrote on 17 apr 2006 in comp.lang.javascript :
Christoph wrote:
I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be working correctly:
if( this.value != '' ) { if (this.value.length > 200) { alert(...); } }
I don't think you need a regular expression to test the number of characters.
and why testing for zero length twice?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
"Christoph" <jc*****@yahoo.com> writes: I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters.
if( this.value != '' ) { if( !( RegExp( '^[.]{0,200}$' ).test( this.value ))) {
As Martin Honnen said, this.value.length is much more efficient.
The problem with your regexp, just for completeness, is that
the character class "[.]" matches exactly one ".", so you
check against strings of "."'s of length 0..200. What you would
have wanted, were you to use a regexp for this, is
RegExp('^.{0,200}$')
Actually, since you are using it on a textarea, which can contain
newlines, you don't want to use ".", since it only matches non-
newlines. Instead, you can use:
RegExp('^[\\s\\S]{0,200}$')
I also recommend using literal regexp notation. It saves on the
backslashes:
/^[\s\S]{0,200}$/
I'm attaching the code to the OnBlur event.
Dangerous. You are effectively preventing the user from having focus
leave the textarea. It's much more pleasent to be allowed to leave
fields invalid while you work on other fields. I recommend marking the
field as invalid somehow (red border, small warning icons, whatever
suits your page's style) and only preventing form submission by testing
in the form's onsubmit handler too.
The test is passed if there is no data in the text area. It fails if there is data, regardless of what that data consists of.
Try with the string "....." :)
I thought the dot matched any character?
It does, on its own. Inside a character class ("[".."]") it has
no special meaning.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
> I don't think you need a regular expression to test the number of characters.
Indeed. It's definitely been one of those mondays. Thanks for
bringing me back down from overcomplication. :p
thnx,
Christoph
Lasse Reichstein Nielsen wrote: "Christoph" <jc*****@yahoo.com> writes: I thought the dot matched any character?
It does, on its own. Inside a character class ("[".."]") it has no special meaning.
Just for completeness, even standalone the dot does
not match any character. It does not match a newline
character.
PointedEars This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Lukas Holcik |
last post by:
Hi everyone!
How can I simply search text for regexps (lets say <a
href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a
dictionary { name : URL}? In a single pass if it could....
|
by: serpent17 |
last post by:
Hello all,
I have this line of numbers:
04242005 18:20:42-0.000002, 271.1748608, , ,
repeated several times in a text file and I would like each element to
be part of a vector. how do...
|
by: McKirahan |
last post by:
How would I use a regular expression to remove all trailing Carriage Returns
and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance.
Also, are they any great references for learning...
|
by: Jeff Sandler |
last post by:
I have a page that accepts input from many textboxes. Many of the
textboxes are intended to accept dates and times, thus, I expect only
digits to be entered. I originally tested using parseInt...
|
by: VK |
last post by:
Hello!
Given a string like
<input type="button" name="b1" value="Button">
I want to locate the element's name (b1) and replace it with this very name
+ some spice.
And of course it can be...
|
by: weston |
last post by:
Has anyone ever encountered trouble with regular expressions not
capturing matches specified by parentheses? I seem to have a weird
situation where a regular expression is matching the String I'm...
|
by: Chris Croughton |
last post by:
I'm trying to use the EXSLT regexp package from
http://www.exslt.org/regexp/functions/match/index.html
(specifically the match function) with the libxml xltproc (which
supports EXSLT), but...
|
by: c676228 |
last post by:
Hi everyone,
I just realized that it's so important to validate each string, I mean
'each' before you insert data from asp page into database.
I guess some customers just copy data from some...
|
by: =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= |
last post by:
With regexps you can search for strings matching it. For example,
given the regexp: "foobar\d\d\d". "foobar123" would match. I want to
do the reverse, from a regexp generate all strings that could...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |