Monday, 23 September 2019

Regular expression in c# - 7 digits with 2 decimal places


Regular expression  in c# - 7 digits with 2 decimal places
eg: 1234567.12
or 123.12
or 123
or 123.11


^-?(0|[1-9]\d{0,6})([,\.]\d{1,2})?$


BEFORE COMMA:

^ - beginning of the string

-? - there may be zero or one negative sign

0 - decimal may starts with only single zero at the beginning...

| - or ...

[1-9] - with any digit from 1 to 9

\d{0,6} - followed by any 7 digits

AFTER COMMA:

[,.] - there may be "," or "."

\d{1,2} - followed by 1 or 2 digits

(...)? - zero or one time

$ - end of the string

Wednesday, 18 September 2019

Factory Pattern - Live example


Factory Pattern - Live example


Definition:

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Example: Download

Taken a abstract class/interface which contain all factory methods(definitions/signatures).

Base Class: declare all abstract methods

Child Class 1: inherit base class
Child Class 2: inherit base class
In the creator class, object instance will create dynamically based on configuration.




Monday, 9 September 2019

Check if an object exists in a JavaScript array using jQuery


Check if an object exists in a JavaScript array using jQuery

If you would like to find an object in existing JavaScript array, below is the sample example.

var list= [];

var obj1 = {id:1,name:'paramesh'};

var exist=$.grep(list, function(obj) {
  return obj.name === 'john'; 
});

if(exist.length===0){
  list.push(obj1);
}