Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts

Tuesday, January 2, 2018

How to use ng-pattern to validate the phone format in AngularJS Web application.

we can directly apply the regular expression pattern to the ng-pattern directive to validate the phone number format. however this approach will limit to mandatory field only. you will see the annoying message if you use this implementation.

<input type="text" ng-model="phoneNumber" id="input" name="phoneNumber" 
 ng-pattern="/^\(?(\d{3})\)?[ .-]?(\d{3})[ .-]?(\d{4})$/" /><br>

we can use different approach to handle this validation, we will use ng-required to validate the required validation, then we will implment a function to take care of phone format validation.



<input type="text" name="phoneNumber" ng-model="phoneNumber" ng-required="requiredCondition" ng-pattern="phoneNumberPattern" />

in the controller javascript file we will implement a validatePhoneNumber function, which will only trigger when the phone field is not blank.

$scope.validatePhoneNumber = (function () {
        var regexp = /^\(?(\d{3})\)?[ .-]?(\d{3})[ .-]?(\d{4})$/;
        return {
            test: function (value) {
                if (value.length == 0) {
                    return true;
                }
                return regexp.test(value);
            }
        };
    })();






 
 





Monday, November 18, 2013

How to use regular expression to trim leading and trailing characters of a specific character in C#

After i enabled filter on the Telerik Grid, the grid filter was not function property, when i copied and paste those search criteria into the filter textbox.

the root cause of this issue is that when i highlight and copy the context in the grid. the context was copied with space in the front and end.  I must trim those leading and trailing space

the following is the filter syntax that i grab via the visual studio debugger

MyColumnName.Contains("  MySearchCriteria  ")

so I need to clear the space before and after double quotation mark.

the regular expression is the good fit for this issue.

first i will define the regular expression pattern to represent the issue.

s*: space appears 0 or more in the string.
\s*\""\*: space before and after the double quotation mark.

var slashRegex = new Regex(@"(?:\s*\""\s*)+");
 
var filterExpression=MyColumnName.Contains("  MySearchCriteria  "); 
  
filterExpression = slashRegex.Replace(filterExpression, "\"");
 

after i apply the regular expression. 


MyColumnName.Contains("MySearchCriteria")