Monday 25 September 2017

Validate a URL field

This set of code will validate the multi line text field and checks whether the URL entered  is valid or not. If not valid, disable the "Submit" button.


$(document).ready(function () {

    validateURL();


});

// This method is used to validate the url. If url is not valid, disable the submit button.
function validateURL() {

    $("#txtURLField").change(function () {
        if (checkUrl($("#txtURLField").val())) {
            $("#btn-id-submit").prop('disabled', false);
        }
        else {
            $("#btn-id-submit").prop('disabled', 'disabled');
            alert("Please enter a valid URL");
        }
    });
}

// This method has the URL validation properties.
function checkUrl(url) {
    //regular expression for URL
    var pattern = /^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-z]{2,4}/;

    if (pattern.test(url) || (url == "")) {
        return true;
    } else {
        return false;
    }

}

------------------------------------------END------------------------------------------------

No comments:

Post a Comment