|
|
|
(JavaScript) Example: Numbers Only + Current format ! |
|
|
|
|
|
|
|
ตัวอย่าง javascript check number + comma ครับ
ขอขอบคุณบทความของ thaicreate ทุกท่านครับ
เครดิต : thaicreate.com
ทดสอบ : Firefox14.0.1, Chomre, IE 7 8 9 ครับ
Code (PHP)
<!DOCTYPE html>
<html>
<head>
<title>Numbers Only</title>
<style type="text/css">
body{font-family:Arial;font-size:12px;text-align:center}
label{width:150px;text-align:right;display:inline-block;font-weight:bold;}
input[type="text"]{width:200px;border:solid 1px #CCC;}
input[type="submit"]{font-family:Arial;font-size:12px;}
div#band{color:#F00;text-align:right;font-weight:bold;}
</style>
<script type="text/javascript">
function Numbers(e){
var keynum;
var keychar;
var numcheck;
if(window.event){
//IE
keynum = e.keyCode;
}else if(e.which){
// Netscape/Firefox/Opera
keynum = e.which;
}
if(keynum != 8 && keynum != 46 && ( keynum < 48 || keynum > 57 )){
return false;
}
}
function dokeyup( obj, e ){
var keynum;
if(window.event){
// IE
keynum = e.keyCode;
}else if(e.which) {
// Netscape/Firefox/Opera
keynum = e.which;
}
if( keynum != 37 & keynum != 39 & keynum != 110 ){
var value = obj.value;
var svals = value.split( "." ); //Cut decimal point
var sval = svals[0]; //Integer
var n = 0;
var result = "";
var c = "";
for ( a = sval.length - 1; a >= 0 ; a-- ){
c = sval.charAt(a);
if ( c != ',' ){
n++;
if ( n == 4 ){
result = "," + result;
n = 1;
};
result = c + result;
};
};
if ( svals[1] ){
result = result + '.' + svals[1];
};
obj.value = result;
};
};
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="band">Monster Team.</div>
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
<label>Numbers:</label>
<input type="text" name="txt_num" id="txt_num" onkeyup="dokeyup(this, event);" onchange="dokeyup(this, event);" onkeypress="return Numbers(event)" />
<input type="submit" value="Show" />
</form>
<hr />
<?php
echo "value: ";
echo $_REQUEST["txt_num"];
echo "<br />";
echo "value: ";
echo str_replace(",", "", $_REQUEST["txt_num"]);
?>
</body>
</html>
Tag : PHP, JavaScript
|
|
|
|
|
|
Date :
2012-08-01 11:07:42 |
By :
popnakub |
View :
3554 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เยี่ยมครับ เดียวจะทำ Ref จากบทความหลักให้ครับ
|
|
|
|
|
Date :
2012-08-01 20:51:49 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
|
|
|
|
|
Date :
2012-08-01 23:49:13 |
By :
popnakub |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ กำลังหาอยู่พอดีเลย
|
|
|
|
|
Date :
2012-08-02 15:26:51 |
By :
vissarud |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|