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