Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MORE ACTION, LESS COMPUTATION PLEASE!!! 1

Status
Not open for further replies.

bublathejuggla

Technical User
Sep 21, 2002
16
GB
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 == &quot;&quot;) {
alert(&quot;Please enter a start x-coordinate for this subroute&quot;);
document.form7.elements[obj].focus();
return false;
}
if(document.form7.elements[obj].value.length > 5) {
alert(&quot;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&quot;);
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
 
You should look into using regular expressions to validate this, that way you can do the validation in a single line of code (after setting up the regexp)

It should look something like this:
Code:
var validatePhone
validatePhone = /\d{3}/
if((validate.test(yourString)) && (yourString.length == 3)){
   //this is a valid three digit number
}
else{
   //this is not a valid three digit number
}

Also, using the elements collection may not be the safest way to do it as you may be getting elements that are not inputs. This may work better:
Code:
var myElem

//inside loop
myElem = document.getElementById(&quot;STARTXCOORD&quot;+i)

Hope this helps,

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
It might also help to use something other then &quot;i&quot; in the inner for loop since &quot;i&quot; is used in the outer loop. It may be interfering with the outer loop. ________________________
JoelMac
 
Yes joelmac! This same problem once nearly drove me insane! The perfectly legal syntax:
Code:
for (var i=0; i<length; i++)
should also prevent this, because it forces the i variable to be local and therefore distinguished from i variables in higher level blocks.

(why are we having this conversation inside the JSP forum? oh well...)

petey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top