Friday, December 16, 2022

Detect input text box change

 We can use jQuery to identify input text change as shown below. This method uses setTimeout and clearTimeout to force the change event fire only after user finishes typing.

JS

var timer;
$("#title").on("input", function(e) {
  var textValue = $(this).val();
  if ($(this).data("lastvalue") != textValue ) {

    $(this).data("lastvalue", textValue );
    clearTimeout(timer);

    timer = setTimeout(function() {
      //code to handle change
      console.log(textValue);
    }, 500);
  };
});
HTML
<script src="jquery.min.js"></script>
<input type="text" id="title">

Cheers

Samitha

Wednesday, December 7, 2022

validate input type without the protocol for the url

 We can use jQuery validate to format input URL with a valid URL as shown below


rules: {

    url_field: {

      required: true,

      url: true,

      normalizer: function (value){

                value = value.replace(/^https?:\/\//, '');

                return 'http://' + value;

            }

    }

  }


Cheers

Samitha

Sunday, October 9, 2022

Add or Update query string parameter

 If we want to dynamically add or change query string,  URL API can be used as shown below

const url = new URL(location.href);

url.searchParams.set('queryStringKey', 'queryStringValue');


Cheers

Samitha



Saturday, September 17, 2022

Statistics Tool to Check Website Performance

 This is used for generating different HTTP codes. It can be useful for testing how scripts handle different responses.


Usage:

Add the status code to the URL, 

Ex: httpstat.us/200

More information can be found here.

cheers

Samitha

Tuesday, September 6, 2022

Get Client IP Address Using Jquery

  http://jsonip.com is an free API which will return IP as a json response.


 

Response sample: {"ip":"192.100.200.10","about":"/about","Pro!":"http://getjsonip.com"}

Example

$.ajax({

    type: "GET",

    async: false,

    url: "http://jsonip.com",

    success: function (data) {

        alert(data.ip);

    }

});

Saturday, August 13, 2022

SQL Server error: The server principal is not able to access the database under the current security context

 

This is a common error shown when SQL server is not mapped with the database you need to access. Follow the steps below for a resolution

  • Go to Security folder in the server.
  • Double click it and go to Logins folder.
  • Find your user id and double-click it.
  • Login Properties window will open up.
  • In that go to User Mapping.
  • Tick all the Databases that you want to map with that Login.


This can also be achieved using a SQL (specially if you are restoring a db)

USE dbNamw;
ALTER USER {userName} WITH login = {loginName}

Cheers
Samitha

Sunday, July 10, 2022

JavaScript format Date ( DD/MM/YYYY)

 Following function will check if input date is entered in DD/MM/YYYY format,  It also validates a minimum date that can be entered to an input field (original code from here)


         function IsValidDate(inputDate) {

            const regex = /^\d{2}\/\d{2}\/\d{4}$/;

            var minDate = new Date("01/01/1900 12:00:00");


            if (inputDate.match(regex) === null) {

                return false;

            }


            const [day, month, year] = inputDate.split('/');

            const isoFormattedStr = `${year}-${month}-${day}`;

            const date = new Date(isoFormattedStr);

            const timestamp = date.getTime();


            if (typeof timestamp !== 'number' || Number.isNaN(timestamp)) {

                return false;

            }


            if (isoFormattedStr < minDate.toISOString()) {

                return false;

            }


            return date.toISOString().startsWith(isoFormattedStr);

        }

Cheers
Samitha