All Things Techie With Huge, Unstructured, Intuitive Leaps
Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Javascript Evaluation Errors in Eclipse

I have a big huge app developed in Eclipse Juno.  The app has a lot of stuff including some jQuery and other javascript frameworks.  Every time that I do a build, I get errors in the Problems tab.  When I look at them, they are all errors that Eclipse says that I have in the Javascript libraries, yet they work fine.  So what to do?

The obvious thing is to turn off the javascript evaluation in Eclipse.  This is how you do it:


  1. Right click your project
  2. Select Properties -> JavaScript -> Include Path
  3. Select Source tab. ( It's identical to Java Build Path Source tab )
  4. Expand JavaScript source folder
  5. Highlight Excluded pattern
  6. Click Edit button
  7. Click Add button next to Exclusion patterns box. (You can either use Ant-style wildcard patterns or click Browse button to mention the JavaScript file by name).
Hope this helps someone.

Uncaught ReferenceError: $ is not defined?

Okay, so I had a onload javascript function with a paramter called in a jsp.  I got the parameter from a URL.


<%

String thisParam = request.getParameter("myParam");

%>


Immediately after this line, I called a onload function passing the above parameter:

<body onload="call(<%=thisParam%>)">

I kept getting the value of thisParam is not defined.  Uncaught Reference Error.

So what cured it?

I was missing the single quotes:

<body onload="call('<%=thisParam%>')">

Hope this helps someone.

Javascript -- Round Number to nearest 100 -- Validate

I have this web page that is created by a jsp.  The user has to input an amount of money to make a bid, but I don't want the users entering silly amounts like $1098.66.  I want the amount to be in exact increments of $100.  So I have an input tag "<input tabindex='1' type='text' name='amt' id='amt' value='00.00'  size='10' />" and I want to validate and make sure that the number is an increment of $100.  When the user presses the submit button, I call onclick="javascript:validateAndSubmit()".

My validate and submit function is something like this:


var bestPrice = document.(insert Form Name).amt.value;
                //get rid of the dollar sign
var match= bestPrice.match(/[0-9,.]*/);
if (match!==null) {
                    //Make the number into a float
   var amount= parseFloat( match[0].replace(/,/g, '') ); 
                 //find out if the number is not in multiples of 100 using modulus
   var rem = amount % 100;
   if ( rem > 0)
    {
    alert("Your offer must be in multiples of $100.")
    }

Hope that this helps.

Javascript - How To Format a Telephone Number

This is quite a common problem. You have an user input table on your html or jsp page and you want to make sure that it is numbers only, has the area code, and is in a consistent format when you put it into the database. I downloaded some javascript to do just that and it worked fine for numbers that you typed in using the numbers on the keyboard. But when you used the numeric keypad on the keyboard, or a USB external keypad, it failed. The keypad, instead of putting numbers, put junk into it.

So this is how I solved it. I created a javascript file called phone.js. This is the contents:

function formatPnum(phonenum, textbox) {
var regexObj = /^(?:\+?1[-. ]?)?(?:\(?([0-9]{3})\)?[-. ]?)?([0-9]{3})[-. ]?([0-9]{4})$/;
if (regexObj.test(phonenum)) {
var parts = phonenum.match(regexObj);
var phone = "";
if (parts[1]) {
phone += "(" + parts[1] + ") "; }
phone += parts[2] + "-" + parts[3];
textbox.value = phone;
}
else {
//invalid phone number
alert('Please enter a proper phone number');
textbox.value = '';
}
return false;
}

Then at the top of the html page, I added the following script tag:

(script type="text/javascript" src="phone.js" /)

Note: I used "(" and ")" instead of "<" and ">" because this blog actually parses it as real HTML. Then, everywhere I needed a phone number, I added this to the tag (in this case for a fax number):


Fax (Area Code + #)
(input type="text" name="fax" onfocus="this.value='';" onmouseout="javascript:return formatPNum(this.value, this);")


Note: I used "(" and ")" instead of "<" and ">" because this blog actually parses it as real HTML.
You will note, that I have an onfocus event. When the user clicks on it, the previous number or whatever is in the input box goes away. Then the user enters the number. If any part of it fails (like he adds a letter), it fails and the alert shows that there is an error. There is no input displayed. However, if the user enters a three digit area code and a seven digit number, the phone number is automagically formatted into (123) 456-7890.

Piece of cake. Hope this helps.

Javascript: Getting Rid of Unwanted Characters in Number Field


I had a field on a web page where a person would fill in their tax rate as a percentage. Of course, people would put in the percent sign ( % ) along with the number and it would choke the web page. So I needed a quick javascript function to strip out unwanted characters, and leave just numbers and the period if it were a decimal number.

Here is the snippet required. I take the form input. The form name is refForm and taxRate is an input value. The ^ is a negation sign in javascript so I am taking everything out except numbers and decimal.

var si = document.refForm.taxRate.value.replace(/[^\d.]/g, "");
document.refForm.taxRate.value = si;

I assign the original value to a variable and then pass the value back to taxRate after the characters are gone.

Easy way to replace all characters and just leave the numbers using javascript.

Javascript: Uncaught SyntaxError: Unexpected token ILLEGAL


Notes to self: If you get this error on your javascript console:

"Uncaught SyntaxError: Unexpected token ILLEGAL"

The first thing to try, is that if you are passing a string as a parameter, put it single quotes.

This drained me for an hour.