Friday, January 16, 2009

Checks if a given value is Number or not in Javascript



The other day I needed to check via javascript if the text entered in a textbox is a valid or not. I know I can use the built-in isNaN function, but wanted to make sure that would in work all the scenarios. So, I searched online, but I found a couple of examples on the web, but they were using regular expression to check. I am not sure why they are using regular expressions, when you have isNaN function.

I created a test function to make sure isNaN works in all the scenarios and found that it works for almost everything I tried except for three values, they are null, empty string and a string with just spaces. For these three inputs, it returns false but you really want it to return true since obviously they are not valid numbers. I wrote an helper function to get around that and the following page shows the helper method as well as the test function I wrote to validate this helper function:

// Checks if a given value is a valid number or not
// The argument could be any of the following:
// a) A number itself
// b) Number in quotes, i.e. number stored as string
// c) Number could be in scientific format
// d) An object

function isNumber(val)
{
  // we need to explicitly handle null values
  // because isNaN returns false when it should return true 
  if (null == val) return false;
 

  // if it is an empty string or a string with just spaces
  // isNaN returns false, but we really need it to return true
  // this expression will remove spaces if the given value is a string type
  if (typeof(val) == "string")
   val = val.replace(/\s*/g, "");
  
  if (val == "") return false;

  return !isNaN(val);
} 


// test function to test the isNumber function
function isNumber_Test(val, expResult)
{

  var dblQuote = '';
  if (typeof(val) == "string")
   dblQuote = '"';

  var actResult = isNumber(val);

  var clr = 'green';
  if (actResult != expResult)
   clr = 'red';

  document.write('
');
  document.write('isNumber(' + dblQuote + val + dblQuote + ')');
  document.write('' + actResult + '');
  document.write('' + expResult + '');
  document.write('
');
}
By the way, I am using a Javascript utility called SyntaxHighlighter, to render this code. If you have not already used this utility, I would highly recommend you check it out.

Here are some test results of isNumber() function:

Test isNumber() Function


Expression Result Expected Result

Happy coding,
Sonny

Friday, November 21, 2008

Parser Error Message: Access to the path ... (ASP.NET)

I have been working on an ASP.NET page and all of a sudden, I was getting Parse Error on this page as shown below:

All I did was modify the code in the page and plus I could build the page in Visual Studio 2008 with out any errors. I could also build the entire web site with out any errors, but I was still getting the error.

I compared the file with the previous version and I do not see any special characters or anything like that. I even tried removing the using statements and putting them back again thinking there may be some hidden character on the first line, but still did not help.

I finally, made a copy of that file and got the previous version from the Source Control and then the error is gone. I then deleted the previous version and renamed the file (that was giving the problem) to its original name and the error is still gone.

Bottom line, I do not know what caused it and neither do I know how doing what I just did fixed the problem, but thought putting it here would might come handy for anyone that run into this issue.

After I did this, I also found this blog post and the author of this post had to reset the permissions of the file that was causing this error.

Have fun coding.
Sonny

Saturday, November 1, 2008

Using AJAX 3.5 toolkit with latest version of SharpForge 0.5.14b

I just installed the SharpForge version 0.5.14b on my system that has .NET Framework 3.5 and Visual Studio 2008 + Visual Studio SP1. That is, I have AJAX toolkit version 3.5 running and when I browse to the SharpForge web page, I am getting the following error from web.config:

I tried changing the version # for System.Web.Extensions to 3.5.0.0 everywhere it is referenced in the web.config but I am still getting this version error for other user controls in the app. I searched on the SharpForge site and all it indicated is that you would have to install AJAX 1.1 toolkit and I do not want to install the older version of AJAX along with the latest version.
I finally found few pages on the web that talk about how you can run old web sites that are built using AJAX 1.1 toolkit with the latest version with out having to install the older AJAX toolkit. You basically tell the app to use 3.5 version of System.Web.Extensions every time it needs the older version of System.Web.Extension using runtime section.
Here is a blog post that talks about it:
HOWTO: Running ASP.NET 2.0 Ajax Toolkit 1.0.x in .NET 3.5 / SP1 IIS

I spent quite a bit of time chasing this issue and I hope other people find this solution sooner.

Thanks.

Sonny