Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

06 March, 2014

How To Check Javascript Fuction Available OR Not

Problem:
Some time we need to call javascript function which is based on some condition or may pass as parameter and then call that function. In this case we need to check whether this javascript function available or not if available then we call it otherwise not.

 HTML:
<input id="test" value="Available" onclick="ISfuctionAvailable();" type="button"/>
 
<input id="test1" value="Not available" onclick="fuctionNotAvailable();" type="button"/>

Javascript:
function ISfuctionAvailable() {
    if (typeof fuctionNotAvailable == 'function') {
        alert('Function available');
        //You can call function like  fuctionNotAvailable();
    }
    else {
        alert('Function Not available');
    }
}
 
function fuctionNotAvailable() {
    if (typeof test5 == 'function') {
        alert('Function available');
    }
    else {
        alert('Function Not available');
    }
}

Demo:
http://jsfiddle.net/patelriki13/drwdG/




















02 March, 2014

How to pass an event object to a function in Javascript?

HTML:
<input type="button" value="click me" onclick="ButtonClick(event);" /> 
 
Javascript:
function ButtonClick(e, button) {
    alert(e.type);
    alert("Button Click...!");
}

Pass "event" keyword in onclick function and also you pass extra argument after that like


HTML:
<input type="button" value="click me" onclick="ButtonClick(event, this);" /> 
 
Javascript:
function ButtonClick(e, button) {
    alert(e.type);
    alert("Button Click...!");
}

Demo:
http://jsfiddle.net/patelriki13/2zuef/








21 February, 2014

Pass Javascript Array to Controller via AJAX in MVC


Create JavaScript Array: 
var stringArray = new Array();
stringArray .push("1");
stringArray .push("2");
stringArray .push("3");
Ajax Call:
    $.ajax({
        type: 'POST',
        url: '/Controller/View',
        traditional: true,
        dataType: "json",
        data: { methodParam: stringArray },
        success: function (data) {
            alert("success");
        },
        error: function (args) {
            alert("Error on ajax post");
        }
    })
Controller: 
[HttpPost]
public ActionResult TutorAvailability(string[] methodParam)

28 March, 2013

How to check date is valid or not in Javascript

Features:
 
  • Date format: MM/DD/YYYY
  • Text box control values can be validated as correct date
  • Month can be entered compulsory with two digits either 0 or 1 as first digit. So possible invalid entries are from 13 to 19.
  • Date can be entered compulsory with two digits either 0,1,2 or 3 as first digit. So possible invalid entries are from 32 to 39.
  • Entry will be validated on blur event. In case of invalid date then blank TextBox.
  • It will also validate for Leap years for Feb month. Also, it will validate 31 date for April, June, Sep & Nov months.

JavaScript Function:

function isDate(txtDate) {
    var currVal = txtDate.val();
    if (currVal == '') {
        txtDate.val('');
        return false;
    }
 
    //Declare Regex 
    var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var dtArray = currVal.match(rxDatePattern); // is format OK?
 
    if (dtArray == null) {
        txtDate.val('');
        return false;
    }
 
    //Checks for mm/dd/yyyy format.
    dtMonth = dtArray[1];
    dtDay = dtArray[3];
    dtYear = dtArray[5];
 
    if (dtMonth < 1 || dtMonth > 12) {
        txtDate.val('');
        return false;
    }
    else if (dtDay < 1 || dtDay > 31) {
        txtDate.val('');
        return false;
    }
    else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31) {
        txtDate.val('');
        return false;
    }
    else if (dtMonth == 2) {
        var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
        if (dtDay > 29 || (dtDay == 29 && !isleap)) {
            txtDate.val('');
            return false;
        }
    }
    return true;
}