Archive
Separating alphabets and digits from Alphanumeric string– C#
We got a requirement to separate alphabets and digits from alphanumeric string as groups.
Let’s say my alphanumeric string is “Hel00Wor11DD” and I need to get
- Alphabet group as “Hel,Wor,DD”
- Digit group as “00,11”.
Below is the C# code which use Regular expressions and achieve the same
var digitGroup = newList<string>();
var alphabetGroup = newList<string>();
Match regexMatch = null;
string myString = “Hel00Wor11DD”;
while (myString.Length > 0){
if ((regexMatch = Regex.Match(myString, “\\d”)).Success){
// If myString is not starting with digit
if (regexMatch.Index > 0) {
alphabetGroup.Add(myString.Substring(0, regexMatch.Index));
}
// If myString is starting with digits but has subsequent alphabets
elseif ((regexMatch = Regex.Match(myString, “\\D”)).Success) {
digitGroup.Add(myString.Substring(0, regexMatch.Index));
}
// If myString only has digits, no more alphabets
else{
digitGroup.Add(myString.Substring(0));
// No more alphabets
break;
}
myString = myString.Substring(regexMatch.Index);
}
// There are no digits in myString
else{
alphabetGroup.Add(myString);
// No more digits
break;
}
}
When you run above code, you would get Alphabets & Digits separated as Lists.
🙂
Using Regular Expression and JScript to validate phone no format
Hi,
I got a requirement to validate a CRM field with “Indian Telephone Number” format (i.e., 040-1234567).
I opted to use regular expression and Jscript for format validation.
Below is the JScript function
function businessPhoneOnChange() {
var telephone = Xrm.Page.data.entity.attributes.get(“telephone1”);
// Regular expression with Indian Telephone format
var regExp= /^[0-9]\d{2,4}-\d{6,8}$/;
if (telephone) {
var phoneNumber = telephone.getValue();
if (phoneNumber.search(regExp) == -1) {
alert(“Invalid Phone number format”);
// Clear the field value
phoneNumber.setValue(“”);
}
}
}
- Add a webresource with this function to CRM
- Register “onchange” event of field with the above method
- Save & Publish
Hope it helps 🙂