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

Search

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