Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Friday, August 7, 2009

Avoid Special Character using Javascript with Regular Expression

javascript validation for special charcter.


function checkSpecial(obj,objEvent)
{
var specialChar = "^[A-Za-z0-9_]+$" //regular expression defining a 5 digit number
var objVal = obj.value;
var objVallen = obj.value.length;
if (objVal != "")
{
if (objVal.search(specialChar) == -1) //if match failed
{
if (objEvent == "keyup") {
if (objVallen > 1) {
obj.value = objVal.substring(0, parseInt(objVallen) - 1);
}
else {
obj.value = "";
}
}
else {
obj.value = "";
}
alert("Invalid character");
obj.focus();
}
}
}

Tuesday, July 7, 2009

RegEx.Split vs. String.Split

String.split has some limitations in the split. but in the Regex.split will split duplicate delimeter.

The delimeter such as ||, ~~ and ::

string[] output  = null;  string inputSentence = "I am a developer|| I work on .Net || " +     "The latest framework available is 3.5";  //the following line will not work 
output = inputSentence .Split("||".ToCharArray());  
//use RegEx Split instead 
output =  System.Text.RegularExpressions.Regex.Split(inputSentence, System.Text.RegularExpressions.Regex.Escape("||"));