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

W

WindAndWaves

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;
}
 
Z

ZER0

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 ^_^;
 
W

WindAndWaves

ZER0 said:
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 ^_^;

Thank you - from zero to hero in a matter of seconds.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 17
Jan 2005 20:29:26, seen in WindAndWaves
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.
 
E

Evertjan.

ZER0 wrote on 17 jan 2005 in comp.lang.javascript:
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)
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 17
Jan 2005 16:52:14, seen in Evertjan.
ZER0 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.
 
E

Evertjan.

Dr John Stockton wrote on 17 jan 2005 in comp.lang.javascript:
That means not (all digits), rather than (not all) digits.

The code requires at least one digit.


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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top