473,396 Members | 1,871 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,396 software developers and data experts.

what does this mean? !/^\d+$/.test(el.value)

Hi Folks

I have inhereted a script that I understand reasonably well, I just do not
understand

!/^\d+$/.test(el.value)

what the hell does that mean?

Below is the script (there are really three and they validate a four items
on a form (min and max rate, (should be between 0 and 99999) and min and max
rooms (should be between 0 and 99)).

TIA

- Nicolaas
function valid(form) {// thoroughly validates form
var e = 0
var M = ''
//M = BasicCheck (form)

// start checks

var field = form.max_rooms;
var vm = parseInt(field.value, 10);
if (!vm && vm !=0) {
M = M + " You have to indicate the maximum number of rooms - enter 99 to
ensure all properties are included.";
field.focus();
field.select();
e = e + 1
} else if (vm >= 0 && vm < 100){
//do nothing
} else {
M = M + " You have to indicate the maximum number of rooms - enter 99 to
ensure all properties are included. You entered " + vm + ".";
field.focus();
field.select();
e = e + 1;
}

var field = form.min_rooms;
var vi = parseInt(field.value, 10);
if (!vi && vi !=0) {
M = M + " You have to indicate the minimum number of rooms - enter 0 to
ensure ensure all properties are included.";
field.focus();
field.select();
e = e + 1
} else if (vi >= 0 && vi < 100){
//do nothing
} else {
M = M + " You have to indicate the minimum number of rooms - enter 0 to
ensure all properties are included. You entered " + vi + ".";
field.focus();
field.select();
e = e + 1;
}

if (vm <= vi){
M = M + " the maximum number of rooms should be greater than the minimum
number of rooms. "
field.focus ();
field.select ();
e = e + 1
}

var field = form.max_rate;
var vm = parseInt(field.value, 10);
if (!vm && vm !=0) {
M = M + " You have to indicate the maximum tariff in New Zealand dollars
(e.g. 300) - enter 99999 to ensure that all properties are included.";
field.focus();
field.select();
e = e + 1;
} else if (vm >= 0 && vm >= 99999) {
// do nothing
} else {
M = M + " You have to indicate the maximum tariff in New Zealand dollars
(e.g. 300) - enter 99999 to ensure that all properties are included. Yyou
entered " + vm + ".";
field.focus();
field.select();
e = e + 1;
}
var field = form.min_rate;
var vi = parseInt(field.value, 10);
if (!vi && vi !=0) {
M = M + " You have to indicate the maximum tariff in New Zealand dollars
(e.g. 300) - enter 99999 to ensure that all properties are included.";
field.focus();
field.select();
e = e + 1;
} else if (vi >= 0 && vi >= 99999) {
// do nothing
} else {
M = M + " You have to indicate the minimum tariff in New Zealand dollars
(e.g. 150) - enter 0 to ensure that all properties are included. You entered
" + vi + ".";
field.focus();
field.select();
e = e + 1;
}

if (e < 1) { //no errors occured
// do nothing
} else {
document.getElementById('err').innerHTML = M;
}
}

function BasicCheck(form){ //validates booking form
var a=[], msg=[], index=1, e = 0 ;

// re-test the fields
a[a.length]=check(form.elements["min_rate"], "minimum rate", "empty",
"5num");
a[a.length]=check(form.elements[1], "maximum rate", "empty", "5num");
a[a.length]=check(form.elements[2], "minimum number of rooms", "empty",
"2num");
a[a.length]=check(form.elements[3], "maximum number of rooms", "empty",
"2num");

//analyse the tests
for(var ii=0; ii<a.length; ii++)
if(a[ii].error){
msg[msg.length]=a[ii].error;
}
//alert the message, if any
if(msg.length) {
e = e + 1
document.getElementById('errormsg').innerHTML = (msg.join("\n"));
//handle form's submission
return !msg.length;
}
}
function check(el, field) {
//checks a field, e.g. check(form.elements[0], "fieldname", "empty",
"2num"); el = element number
//controller
var result={error:""};
for(var ii=2; ii<arguments.length; ii++)
if((result=singleCheck(el, field, arguments[ii])).error)
break;
return result;
//single check
function singleCheck(el, field, type) {
var result={error:""};
switch(type) {
case "empty" :
if(/^\s*$/.test(el.value)) {
result.error= field+": should be completed.<BR>";
}
break;
case "2num" :
if(!/^\d+$/.test(el.value)) {
result.error=field+": should contain up to two digits only (e.g.
01).<BR>";
} else {
el.value=padLeft(el.value.replace(/^\d+(\d\d)$/,"$1"), "0", 2);
}
break;
case "5num" :
if(!/^\d+$/.test(el.value)) {
result.error=field+": should contain up to five digits only (e.g.
01).<BR>";
} else {
el.value=padLeft(el.value.replace(/^\d+(\d\d)$/,"$1"), "0", 5);
}
break;
}
if(el.style){
el.style.color = result.error ? "#c00" : "";
}
return result;
}
}
function padLeft(str, pad, count) { //turns a number into a string
while(str.length<count)
str=pad+str;
return str;
}


Jul 23 '05 #1
6 23322
On Mon, 17 Jan 2005 20:29:26 +1300, WindAndWaves wrote:
I have inhereted a script that I understand reasonably well, I just do not
understand

!/^\d+$/.test(el.value) what the hell does that mean?


This is a Regular Expression. In that case, check if the property value of
"el" contains only number chars (\d+) , from begin (^) to end ($) of the
string.
As you see, the result is "true" only for positive integer.

P.S.
Sorry for my english ^_^;

--
ZER0

~ The Tangent Universe collapsed 5923 days, 1 hours, 57 minutes and 13 seconds ago.

Jul 23 '05 #2

"ZER0" <ze********@libero.it> wrote in message
news:pv***************@ID-171124.news.individual.net...
On Mon, 17 Jan 2005 20:29:26 +1300, WindAndWaves wrote:
I have inhereted a script that I understand reasonably well, I just do not understand

!/^\d+$/.test(el.value)
what the hell does that mean?


This is a Regular Expression. In that case, check if the property value of
"el" contains only number chars (\d+) , from begin (^) to end ($) of the
string.
As you see, the result is "true" only for positive integer.

P.S.
Sorry for my english ^_^;

--
ZER0

~ The Tangent Universe collapsed 5923 days, 1 hours, 57 minutes and 13

seconds ago.


Thank you - from zero to hero in a matter of seconds.
Jul 23 '05 #3
ZER0 wrote on 17 jan 2005 in comp.lang.javascript:
On Mon, 17 Jan 2005 20:29:26 +1300, WindAndWaves wrote:
I have inhereted a script that I understand reasonably well, I just
do not understand

!/^\d+$/.test(el.value)

what the hell does that mean?


This is a Regular Expression. In that case, check if the property
value of "el" contains only number chars (\d+) , from begin (^) to end
($) of the string.
As you see, the result is "true" only for positive integer.


result = !/^\d+$/.test(el.value)

is testing for "not all numbers" in the string.

The same result and simpler is testing for "minimal one not-a-number":

result = /\D/.test(el.value)
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4
JRS: In article <_S*******************@news.xtra.co.nz>, dated Mon, 17
Jan 2005 20:29:26, seen in news:comp.lang.javascript, WindAndWaves
<ac****@ngaru.com> posted :

I have inhereted a script that I understand reasonably well, I just do not
understand

!/^\d+$/.test(el.value)

what the hell does that mean?


It means : not one-or-more decimal digits and nothing else in el.value.

If number zero is not allowed, [1-9]\d* would be better than \d+ .
If large numbers like 1000000000 are not allowed, use \d{m,n} to specify
the allowed number of digits.

The inherited code is wastefully repetitive; factor out common parts.

Don't allow your posting system to wrap code lines; see FAQ. Code
should be executable as transmitted.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

Rather than giving long-winded error messages, repetitively coded, it is
better to make sure that the HTML clearly indicates the requirements for
each field of the form - plain numbers, 0 <= minrate <= maxrate, 0 <=
minrooms <= maxrooms . It is then at most necessary to indicate which
field is in error, and perhaps whether the complaint is of format or
value.

However, with ranges of 0-99, 0-99999 there is no need to check values
(except that max>=min) since by allowing only 1-2 or 1-5 digits the
range is already enforced.

Scrap the code, and rewrite it from the beginning.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5
JRS: In article <Xn********************@194.109.133.29>, dated Mon, 17
Jan 2005 16:52:14, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
ZER0 wrote on 17 jan 2005 in comp.lang.javascript:
On Mon, 17 Jan 2005 20:29:26 +1300, WindAndWaves wrote:
I have inhereted a script that I understand reasonably well, I just
do not understand

!/^\d+$/.test(el.value)
what the hell does that mean?


This is a Regular Expression. In that case, check if the property
value of "el" contains only number chars (\d+) , from begin (^) to end
($) of the string.
As you see, the result is "true" only for positive integer.


result = !/^\d+$/.test(el.value)

is testing for "not all numbers" in the string.


That means not (all digits), rather than (not all) digits.

The code requires at least one digit.
The same result and simpler is testing for "minimal one not-a-number":

result = /\D/.test(el.value)


That only requires no non-digits; it does not require any digits, and
will accept "". Then parseInt will give NaN, and + will give 0 ; the OP
has a choice to make.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
Dr John Stockton wrote on 17 jan 2005 in comp.lang.javascript:
result = !/^\d+$/.test(el.value)

is testing for "not all numbers" in the string.


That means not (all digits), rather than (not all) digits.

The code requires at least one digit.
The same result and simpler is testing for "minimal one not-a-number":

result = /\D/.test(el.value)


That only requires no non-digits; it does not require any digits, and
will accept "". Then parseInt will give NaN, and + will give 0 ; the
OP has a choice to make.

True.

The OC [original coder] didn't use:

result = !/^\d*$/.test(el.value)

and that is equivalent to:

result = /\D/.test(el.value)

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #7

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

Similar topics

2
by: Don | last post by:
What does the onfocus value of "this.select()" do in the following <input...> tag? <input type=text name="img" size=20 onfocus="this.select()" value="http://" tabindex=7> Thanks, Don ...
3
by: RobertTG | last post by:
Someone please translate the code below into English... Particularly the indicated line Thanks function attachComment() { var aForms = document.getElementsByTagName("FORM"); for (var i = 0;...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
6
by: wintaki | last post by:
Given int x; What does x evaluate to? If x is 0, it equals 0x31, the ascii value of '1'. So for some reason, x evaluates to *(y+x) where x is a non pointer/array type.
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
6
by: **Developer** | last post by:
What does this mean: External component has thrown an exception. My program crashes after it leaves a subroutine. What I see during debugging is when I press F11 at the End Sub statement
2
by: riceyeh | last post by:
Hi, What does <xsl:if test="not($values)"mean? What I do not understand is $values? Here, means array? And . = $value means current node is equal to the variable value? So the total meaning is...
2
by: mantrid | last post by:
in some php script ive seen $something==@$somethingelse whereas something==$somethingelse works the same what does the use of the @ do? Sorry if this is a stupid question
2
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, in a web.config, what does the following mean? allow users="*" Does this mean all intranet users? thanks, rodchar
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: 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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.