RSS
 

Posts Tagged ‘regular expression’

Test if the given value is a decimal number or not in JavaScript using Regular Expression

26 May

To identify in JavaScript whether the number or a string is in a decimal format or not, is actually pretty easy and short.
It looks obvious, but actually it doesn’t due to several different combinations of decimal number representation in JavaScript.

I’m using Regular Expression to test it:

1
2
3
function isDecimal(number) {
    return /^\d+(\.\d+|e)(e?-\d+)?$/.test(number);
}

Read the rest of this entry »

 

PhoneGap – Detect if the application runs on mobile or browser using JavaScript before the “deviceready” and document “ready” events are triggered

05 Nov

I spent hours to find out how to do it. And believe me or not, I found nothing on the whole internet about how to solve it.

Actually there is a simple way to do that after the event “deviceready” has been triggered (as described on the PhoneGap’s API Docs), it is simple as:

1
2
3
4
5
6
7
document.addEventListener("deviceready", function(e){
if ( device ) {
alert("Running on PhoneGap!");
} else {
alert("Running NOT on PhoneGap!");
}
}, false);

Read the rest of this entry »