Archive for July 2015

Replace multi string in Javascript

There is only way is replacing the string by regular expression. For example:

s = s.replace(/yourPaternString/g, 'yourNewValue');
However, the patern string may not work when it contains metacharacter of regex, such as . $ ^ { [ ( | ) * + ? \. We need to escape these characters by following code:
patern = patern.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
text = text.replace(new RegExp(patern, 'g'), 'yourNewValue');

Leave a comment

Split string in SQL Server

Just run this function.
Notice that empty value between delimiter will return NULL, you can customize this by modify the line "insert into @tParts values( null )"

CREATE FUNCTION [dbo].[SplitString]
(
    @sString nvarchar(MAX),
    @cDelimiter nchar(1)
)
RETURNS @tParts TABLE ( part nvarchar(MAX) )
AS
BEGIN
    if @sString is null return
    declare @iStart int,
      @iPos int
    if substring( @sString, 1, 1 ) = @cDelimiter 
    begin
     set @iStart = 2
     insert into @tParts
     values( null )
    end
    else 
     set @iStart = 1
    while 1=1
    begin
     set @iPos = CHARINDEX(@cDelimiter, @sString, @iStart )
     if @iPos = 0
      set @iPos = len( @sString )+1
     if @iPos - @iStart > 0   
      insert into @tParts
      values ( substring( @sString, @iStart, @iPos-@iStart ))
     else
      insert into @tParts
      values( null )
     set @iStart = @iPos+1
     if @iStart > len( @sString ) 
      break
    end
    RETURN

END

Leave a comment

ASP MVC unobtrusive validation for dynamic html

When I add the dynamic content to page with javascript or ajax, the validation doesn't work with new content at all. I solved the problem by 2 ways.

Solution 1: Parse only changed content
$("#yourID").html(yourContent); // add html to your tag
$.validator.unobtrusive.parse($("#yourID")); // this will parse validation for new content
Solution 2: Parse validation for form tag
$.removeData($('#YourFormID').get(0), 'validator'); // clear the validation of the form tag
jQuery.validator.unobtrusive.parse('#YourFormID');

Posted in | Leave a comment

Escape jquery selector value

Declare the escape function for jquery at first

$.escape = function(str) {
  return str.replace(/[!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~]/g, '\\$&');
};
Example code
// the value contain special character
var searchValue = 'your-value*'; 

// this is safe with escape
var list1 = $('[value="' + $.escape(searchValue) + '"]';

// this may error without escape
var list2 = $('[value="' + searchValue + '"]'; 
Try to type the input

This is escaped value

Posted in | Leave a comment

Escape regex string javascript

Declare the escape function at first

RegExp.escape = function(string) {
  return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
And you can search special character like this code
var patern = '^^';
var regex = new RegExp(RegExp.escape(patern));
var text = 'Hello^^';
var match = text.match(regex));
Try to type the input

Try to type the patern with special character

Matched text found:

Posted in | Leave a comment

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.