Search This Blog

Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Wednesday, October 21, 2009

Friday, October 9, 2009

JQuery How to restict text box and text area to enter number

Try out following JQuery script , It will allow to enter 14,3 combination to text box. i.e
decimal number with max 14 (without .) and maximum of 3 decimal.234.

input type="text" id="transAmtField"/

$(document).ready(function() {
restrictInt(14,3);
});
var oldAmount;
function restrictInt(num,decimal) {
var amount;
var newNum=0;
$("#transAmtField").bind('keyup', function(e) {
amount = $("#transAmtField").val();
if(isNaN(amount)) {
$("#transAmtField").val(amount.replace(/[^0-9\.]/g,''));
return false;
}
if(amount.lastIndexOf(".") != -1) {
if(amount.length-amount.lastIndexOf(".") > decimal+1 ){
$("#transAmtField").val(oldAmount);
return false;
} else if(amount.length <= num+1) {
oldAmount = amount;
return true;
} else {
oldAmount = amount.substring(0,num+1);
$("#transAmtField").val(oldAmount);
return false;
}
}
if(amount.lastIndexOf(".") == -1) {
if(amount.length <= num) {
return true;
} else {
oldAmount = amount.substring(0,num);
$("#transAmtField").val(oldAmount);
return false;
}
}
});
}

Thursday, October 8, 2009

Restrict to number Input box using JQuery

We can use following JQuery code to allow only number in input box :


$('#inputId').bind('keypress', function(e) { return ( e.which!=8 && e.which!=0 && (e.which<48

e.which>57)) ? false : true ;})

Monday, September 7, 2009

Select with JQuery

1.Following way we can select any option / value in select using JQuery :



2. Following way we can disable the element :

Friday, September 4, 2009

Three technology to handle Simple Ajax

Recently I have used 3 technology in one portal project on requirement basis for handling ajax calls , explaining all three ways to make ajax calls here . I found that JQuery is most effective and easy to use :

1. Using native ajax :

//Do the AJAX call
if (window.XMLHttpRequest) {

// Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true);
} catch (e) {
alert("Server Communication Problem\n"+e);
}
req.send(null);
} else if (window.ActiveXObject) {
// IE

req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange=processStateChange;
req.open("GET", url, true);
req.send();
}
}

function processStateChange() {

if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response

//Split the text response into Span elements
spanElements =
splitTextIntoSpan(req.responseText);

//Use these span elements to update the page
replaceExistingWithNewHtml(spanElements);

} else {
alert("Problem with server response:\n "
+ req.statusText);
}
}
}

2. Using JQuery :

 $.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
},
error : function() {}
});


3 Using Smart client : Use data source with Json Or XML

isc.DataSource.create({
ID:"contacts",
dataFormat:"json",
dataURL:employee.php",
fields:[
{name:"name"},
{name:"email"},
{name:"organization"},
{name:"phone"},
{name:"street", valueXPath:"address/street" },
{name:"city", valueXPath:"address/city" },
{name:"state", valueXPath:"address/state"},
{name:"zip", valueXPath:"address/zip" }
]
});

Popular Posts