PHP Web Developer.

สิงหาคม 15, 2012

How to allow only one decimal separator in the texbox?

Filed under: Javascript & jquery — Prajak @ 5:19 pm

$(‘.numeric_input’).live(“keyup”,function(){inputControl($(this),’int’);});
$(‘.float_input’).live(“keyup”,function(){inputControl($(this),’float’);});

function inputControl(input,format)
{
var value=input.val();
var values=value.split(“”);
var update=””;
var transition=””;
if (format==’int’){
expression=/^([0-9])$/;
finalExpression=/^([1-9][0-9]*)$/;
}
else if (format==’float’)
{
var expression=/(^\d+$)|(^\d+\.\d+$)|[,\.]/;
var finalExpression=/^([1-9][0-9]*[,\.]?\d{0,3})$/;
}
for(id in values)
{
if (expression.test(values[id])==true && values[id]!=”)
{
transition+=”+values[id].replace(‘,’,’.’);
if(finalExpression.test(transition)==true)
{
update+=”+values[id].replace(‘,’,’.’);
}
}
}
input.val(update);
}

ให้ความเห็น »

ยังไม่มีความเห็น

RSS feed for comments on this post. TrackBack URI

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

บลอกที่ WordPress.com .

%d bloggers like this: