Tuesday, May 8, 2012

Javascript function

function emailCheck() { var email_race_0 = document.getElementById("email_race_0").value; var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(reg.test(email_race_0) == false) { alert('Invalid Email Address'); return false; } }

function isAlphaOrParen(str) { return /^[a-zA-Z()]+$/.test(str); }

function emty_value_space(str_value) { if(str_value=='' || !str_value.match(/[^\s]/)) {return false;}else{return true;} }

function isNumeric(v) { return v.length > 0 && !isNaN(v) && v.search(/[A-Z]|[#]/ig) == -1; };

function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] + '/' + bits[1] + '/' + bits[0]); return !!(d && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[0])); }

//Make clone of same form
var id = document.getElementById('cnt_edit').value; //default zero
var id = id +1;
document.getElementById('cnt_edit').value = id;
var firstform=document.getElementById('firstform').innerHTML;
firstform=firstform.replace(/tik_0/g,"tik_"+id);
$("#new_form").append($(firstform).clone(true));
note : use first form element id like this <input type="text" name="reg[tik_0][title]" id="title_tik_0"  />
//End

How to use
if(!isNumeric(donation_amt.value)) { alert("Please Enter Number"); donation_amt.focus(); return false; }

if(!isAlphaOrParen(individual_last_name.value)) { alert("Please Enter Only Alphabates."); individual_last_name.focus(); return false; }

if(!emty_value_space(individual_last_name.value)) { alert("Please Enter Fundraising Last Name"); individual_last_name.focus(); return false; }

var start_date = jQuery('#start_date').val(); if(!isValidDate(start_date.toString())) { alert("Please enter start date in dd/mm/yyyy format"); jQuery('#start_date').focus(); return false; }

Check whether element is exist in document or not

function isElementInDocument(el) {
    var html = document.body.parentNode;
    while (el) {
        if (el === html) {
            return true;
        }
        el = el.parentNode;
    }
    return false;

}
How to use
var liobj = document.getElementById('sort_1');
alert(isElementInDocument(liobj));

check Price format
var cost = 2.5;
var costRegex = /^\d{0,9}(\.\d{0,2})?$/;

if (!(costRegex.test(cost))) {
        $('#add_store_product_error').html("Please enter cost in correct format ex: '123.55'");
        return false;
}


No comments:

Post a Comment