Thursday 6 October 2022

.Net c# Read html string and get Attribute values of html tags:

public List<int> GetIds()
{
    string html = "<p>hi&nbsp;<a class=\"test\" id=\"8369\">Anil Kumar</a> how are you? <a class=\"test\" id=\"8370\">Manoj Kumar</a></p>";
    List<int> ids = new List<int>();
    string pattern = "<a class=\"test\" id=\"(.*?)\">(.*?)</a>";
    var rows = Regex.Matches(html, pattern, RegexOptions.Multiline);
    if (rows.Count > 0)
    {
        foreach (Match row in rows)
        {
            int id = GetAttributeValue<int>(row.Value, "id");
            ids.Add(id);
        }
    }
    return ids;
}

public static T GetAttributeValue<T>(string htmlString, string attributeName)
{
    string pattern = string.Format(@"(?<={0}="").*?(?="")", attributeName);
    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    Match match = rgx.Match(htmlString);
    return JsonConvert.DeserializeObject<T>(match.Value); //Method of Newtonsoft.Json
}


Monday 22 August 2022

Use of .map in jQuery to get selected checkboxes values:

HTML:
<input type="checkbox" class="chkitem" value="1"/>
<input type="checkboxclass="chkitem" value="2"/>
<input type="checkboxclass="chkitem" value="3"/>

jQuery:
var ids = $(".chkitem:checked").map(function () {
                        return $(this).val();
                    }).get();

Simple C# code to Validating email address

 public static bool ValidateEmail(string email)
 {
      string regex = @"^[a-z0-9][-a-z0-9._]+@([-a-z0-9]+\.)+[a-z]{2,5}$";
       return Regex.IsMatch(email, regex);
  }

Formatting phonenumber by removing all alphabets and characters except "+" and numbers in c#.

        public static string FormatPhoneNumber(string phoneNumber)
        {
            phoneNumber = Regex.Replace(phoneNumber, "[^0-9+]", "");
            if (phoneNumber == "+")
            {
                phoneNumber = string.Empty;
            }
            return phoneNumber;
        } 

Check/Uncheck radio buttons like checkboxes in jQuery:

Html:

<input type="radio" name="gender" value="1" onclick="onChangeRadio(this)" data-prevval="0"/>

<input type="radio" name="gender" value="2" onclick="onChangeRadio(this)" data-prevval="0"/>


Jquery:

function onChangeRadio($this) {

        var productId = $($this).val();

        var checked = $($this).data("prevval");

        if (checked == 1) {

            $($this).prop("checked", false);

            $($this).data("prevval", 0);

        } else {

            $($this).data("prevval", 1);

        }

        var name = $($this).attr("name");

        $("input[name=" + name + "]").each(function () {

            var value = $(this).val();

            if (value != $($this).val()) {

                $(this).data("prevval", 0);

            }

        });

        var option;

        if ($($this).is(":checked")) {

            option = 0;

        } else {

            option = 1;

        }

        //do something

    }

Tuesday 12 January 2021

jQuery server-side datatable: Sending sorting and searching parameters with sort column name to server-side method.

 Add following code in "ajax" option of server side datatable:

"ajax": {
     "url": "/url",
     "type": "GET",
     "data": function (d,e) {
         var requestData = {
             draw: d.draw,
             length: d.length,
             SortColumn: d.order[0]["column"],
             SortColumnName: e.aoColumns[d.order[0]["column"]].sTitle,
             SortDirection: d.order[0]["dir"],
             length: d.length,
             search: d.search.value,
             start: d.start         
        };
         return requestData;
     },
     "dataSrc": function (json) {
         return json;
     }
}

Get count and values of checked checkboxes using jquery:

<input type="checkbox" class="chkitem"/> Item1
<input type="checkbox" class="chkitem"/> Item2
<input type="checkbox" class="chkitem"/> Item3

1. To Get Count:
$('.chkitem:checkbox:checked').length;

2. To get values of checked checkboxes:
var selectedItems=[];
$('.chkitem:checkbox:checked').each(function () {
    selectedItems.push($(this).val());
});