By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,693 Members | 1,799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,693 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.