?
Solved

Comment jquery code

Posted on 2014-07-16
8
Medium Priority
?
185 Views
Last Modified: 2014-07-16
i finally got his page done with all the help from you experts
could someone comment this code, its over my head?
$('#in').on('input propertychange', function () {
    var text = $(this).val();
    var t2 = text.replace(/[^\d\.]/g, "")
        .replace(/\./, "z")
        .replace(/\./g, "")
        .replace(/z/, ".");
 
  t2 = ""||/^(\d+)?([.]?\d{0,2})?$/.test(text)===false?t2 = "Input Numbers Only":t2="";
   
    if(t2==="Input Numbers Only"){alert(t2);}


 $('#in').on('keyup', function() {
                    if ($('#in').val().length > 0) {
                            var strValue = $('#in').val();
                            if (strValue.indexOf('.') > -1) {
                                    if (strValue.split('.')[1].length > 1) {
                                            $('#in').val(strValue.substring(0, strValue.length - 1));
                                    }
                            }
                            var value = parseFloat($('#in').val());
                            if (value > 90000) {
                                    value = Math.floor(value);
                                    value /= 10;
                                    value = Math.floor(value);
                                    value = value.toFixed(0);
                                    $('#in').val(value);
                                    $('#measureLabel').removeClass('Require');
                            }
							roofAreaVal = $('#selectPitch option:selected').val();
                            if (value < 100) {
                                    $('#measureLabel').addClass('Require');
									calc(roofAreaVal); // call calc fnction
                            } else {
                                    $('#measureLabel').removeClass('Require');
									calc(roofAreaVal); // call calc fnction
                            }
                    }
            });

Open in new window

0
Comment
Question by:isnoend2001
  • 4
  • 4
8 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40200207
This line is wrong

 t2 = ""||/^(\d+)?([.]?\d{0,2})?$/.test(text)===false?t2 = "Input Numbers Only":t2="";

should be

 t2 == ""||/^(\d+)?([.]?\d{0,2})?$/.test(text)===false?t2 = "Input Numbers Only":t2="";
0
 
LVL 58

Expert Comment

by:Gary
ID: 40200229
The regex is checking the input is a number with only one period (full stop)

The keyup function checks if there is more than one period in the value, if there is then it removes everything after the second period.


If value is greater than 90000 then round the number down to the nearest integer, divide by 10, round down again, and set the inputs value to this new number

The rest is pretty much self explanatory
0
 

Author Comment

by:isnoend2001
ID: 40200240
thanks Gary
I made the change and it still works
http://roofgenius.com/test2.asp
as far as The rest is pretty much self explanatory, maybe to you but not to me
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 58

Expert Comment

by:Gary
ID: 40200257
What other bits don't you understand
0
 

Author Comment

by:isnoend2001
ID: 40200298
var value = parseFloat($('#in').val());
just about every line
0
 

Author Comment

by:isnoend2001
ID: 40200307
i was hoping to have a comment on each line like this:
  // CHECK FOR A CHANGE IN THE DROP DOWN MENU
     $('#selectPitch').change(function () {
        $('#selectPitchLabel').addClass('Require'); // RESET LABEL
         var roofAreaVal = $(this).val();  // GET VALUE OF DROPDOWN xnew 07-12-14        
         var roofAreaTxt = $('#selectPitch option:selected').text();  //GET VALUE OF THE TEXT OF THE DROP DOWN
         var rat = roofAreaTxt.split(' ');  // CONVERT THE TEXT OF THE DROP DOWN TO AN ARRAY. REQUIRED TO REVERSE USAGE IN HE MESSAGE
         var newAreaTxt = rat[1] + ' ' + rat[0];  // REVERSE THE ORDER OF THE TEXT
             $('#Result').text(roofAreaTxt);   // 7-12-2014 Q_28474675
         if (roofAreaVal != 0) {  // CHECK TO MAKE SURE THERE IS A VALUE IN THE DROP DOWNxnew 07-12-14
              $('#selectPitchLabel').removeClass('Require');//xnew 07-12-14
             
             calc(roofAreaVal);  // RUN THE FUNCTION TO MAKE THE CALCULATIONS
             $('#roofMesg').val('Roof ' + newAreaTxt + ' added ' + roofAreaVal);
         }

     });
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40200337
$('#in').on('input propertychange', function () {
	
     var text = $(this).val(); //Get input value

     Replace anything not a number and replace . with z
     var t2 = text.replace(/[^\d\.]/g, "")
         .replace(/\./, "z")
         .replace(/\./g, "")
         .replace(/z/, ".");
  
   // if the value is blank or fails the number/single period regex then set t2 = Input Numbers Only
   t2 == ""||/^(\d+)?([.]?\d{0,2})?$/.test(text)===false?t2 = "Input Numbers Only":t2="";
    
     //if t2= Input Numbers Only then make an alert
     if(t2==="Input Numbers Only"){alert(t2);}


  $('#in').on('keyup', function() {
                     if the input with ID [i]in[/i] is greater than 0
                     if ($('#in').val().length > 0) {
                             // Get the value
                             var strValue = $('#in').val();
                             
                             //Check if there is a period
                             if (strValue.indexOf('.') > -1) {
                                     // check if there is more than 1 period and remove everything after and including it.
                                     if (strValue.split('.')[1].length > 1) {
                                             $('#in').val(strValue.substring(0, strValue.length - 1));
                                     }
                             }
                             // Make sure the input value is a number
                             var value = parseFloat($('#in').val());
                             if (value > 90000) { //if greater than 90000
                                     
                                     value = Math.floor(value);// round down to a whole number
                                     value /= 10;//Divide by 10
                                     value = Math.floor(value);// round down to a whole number again
                                     value = value.toFixed(0);//Make sure is a whole number (not needed)
                                     $('#in').val(value);// Set the input to the new value
                                     $('#measureLabel').removeClass('Require');// Remove the [i]Require[/i] class
                             }
                                           roofAreaVal = $('#selectPitch option:selected').val();// Get the dropdown value
                             if (value < 100) { //if dropdown is less than 100
                                     $('#measureLabel').addClass('Require'); // Add the [i]Require[/i] class
                                                       calc(roofAreaVal); // call calc fnction
                             } else {
                                     $('#measureLabel').removeClass('Require');// Remove the [i]Require[/i] class
                                                       calc(roofAreaVal); // call calc fnction
                             }
                     }
             }); 

Open in new window

0
 

Author Closing Comment

by:isnoend2001
ID: 40200361
Yes Thank you
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question