bublathejuggla
Technical User
hi all. im using javascript to perform clientside validation of a HTML table of inputs. however, when i use a nested 'for' loop, the javascript utilises all my CPU and pauses indefinately. heres the code:
function validate(no_of_user_profiles) {
startxcoordpart = 'STARTXCOORD';
for(i = 0; i < no_of_user_profiles; i++) {
obj=startxcoordpart+i;
var stringNumber = document.form7.elements[obj].value;
if(document.form7.elements[obj].value == ""
{
alert("Please enter a start x-coordinate for this subroute"
;
document.form7.elements[obj].focus();
return false;
}
if(document.form7.elements[obj].value.length > 5) {
alert("Please enter a start x-coordinate of up to three integers or up to three integers followed by a decimal point and another integer for decimal coordinate values"
;
document.form7.elements[obj].focus();
return false;
}
for(i = 0; i < stringNumber.length; i++) {
if(stringNumber.charAt(i) != '1'
&& stringNumber.charAt(i) != '2'
&& stringNumber.charAt(i) != '3'
&& stringNumber.charAt(i) != '4'
&& stringNumber.charAt(i) != '5'
&& stringNumber.charAt(i) != '6'
&& stringNumber.charAt(i) != '7'
&& stringNumber.charAt(i) != '8'
&& stringNumber.charAt(i) != '9'
&& stringNumber.charAt(i) != '0'
&& stringNumber.charAt(i) != '.') {
alert('Please re-enter a valid mobile phone number');
document.form7.elements[obj].focus();
return false;
}
if(stringNumber.charAt(i) == '.') {
if(i != stringNumber.length-2) {
alert('Please re-enter a valid mobile phone number');
document.form7.elements[obj].focus();
return false;
}
}
}
}
}
as you can see, the validate method takes an integer representing the no. of table rows and then dynamically works out the name of each row's cell by concatenating the row number to 'STARTXCOORD' to get 'STARTXCOORD3', for example. each cell is then referenced by their name, which is consistent with their HTML names.
each cell may only hold:
1) integers up to 3 digits long
2) a decimal number with up to 3 digits before the decimal point followed by another single integer.
can anyone help in finding a cheaper way to implement this? please let me know. bub
function validate(no_of_user_profiles) {
startxcoordpart = 'STARTXCOORD';
for(i = 0; i < no_of_user_profiles; i++) {
obj=startxcoordpart+i;
var stringNumber = document.form7.elements[obj].value;
if(document.form7.elements[obj].value == ""
alert("Please enter a start x-coordinate for this subroute"
document.form7.elements[obj].focus();
return false;
}
if(document.form7.elements[obj].value.length > 5) {
alert("Please enter a start x-coordinate of up to three integers or up to three integers followed by a decimal point and another integer for decimal coordinate values"
document.form7.elements[obj].focus();
return false;
}
for(i = 0; i < stringNumber.length; i++) {
if(stringNumber.charAt(i) != '1'
&& stringNumber.charAt(i) != '2'
&& stringNumber.charAt(i) != '3'
&& stringNumber.charAt(i) != '4'
&& stringNumber.charAt(i) != '5'
&& stringNumber.charAt(i) != '6'
&& stringNumber.charAt(i) != '7'
&& stringNumber.charAt(i) != '8'
&& stringNumber.charAt(i) != '9'
&& stringNumber.charAt(i) != '0'
&& stringNumber.charAt(i) != '.') {
alert('Please re-enter a valid mobile phone number');
document.form7.elements[obj].focus();
return false;
}
if(stringNumber.charAt(i) == '.') {
if(i != stringNumber.length-2) {
alert('Please re-enter a valid mobile phone number');
document.form7.elements[obj].focus();
return false;
}
}
}
}
}
as you can see, the validate method takes an integer representing the no. of table rows and then dynamically works out the name of each row's cell by concatenating the row number to 'STARTXCOORD' to get 'STARTXCOORD3', for example. each cell is then referenced by their name, which is consistent with their HTML names.
each cell may only hold:
1) integers up to 3 digits long
2) a decimal number with up to 3 digits before the decimal point followed by another single integer.
can anyone help in finding a cheaper way to implement this? please let me know. bub