 |
|
|
 |
 |
|
ลองดูแบบนี้
Code
<input type="text" onkeypress="return okp(event)"/>
<script type="text/javascript">
/* Thanks to Dr.Yes for this function .. eiei */<br>
function okp(e) {
if (!document.all && e.keyCode>0) { return true; }
var allowable = '0123456789.';
var c = document.all ? String.fromCharCode(e.keyCode) : String.fromCharCode(e.charCode);
if (allowable.indexOf(c)<0) {
alert('ใส่ตัวเลขได้เท่านั้น');
return false;
} else {
return true;
}
}
</script>
อยากให้ใส่อะไรได้มั่งก็แก้ในตัวแปร
var allowable = '0123456789.';
เช่น อยากเพิ่ม , - @ ก้อใส่เข้าไปแบบนี้
var allowable = '0123456789.,-@';
ลองดูนะครับ
|
 |
 |
 |
 |
Date :
2009-04-02 23:43:47 |
By :
noomna19 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (JavaScript)
function inputDigits(sensor){
var regExp = /[0-9]$/;
if(!regExp.test(sensor.value)){
alert("กรอกตัวเลขเท่านั้นนะจ๊ะ จุ๊บๆ");
sensor.value = sensor.value.substring(0, sensor.value.length -1);
}
}
<input type="text" name="mytextbox" id="mytextbox" onKeyUp="javascript:inputDigits(this);">
|
 |
 |
 |
 |
Date :
2009-04-02 23:47:33 |
By :
plakrim |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากนะค่ะ
|
 |
 |
 |
 |
Date :
2009-04-02 23:57:15 |
By :
Kmay |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วถ้าเป็นอักษรอังกฤษอย่างเดียวล่ะ
|
 |
 |
 |
 |
Date :
2009-10-28 23:16:05 |
By :
ดิง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วเช็คตัวอักษรมีท่านผู้รู้เขียนไว้ไหมครับ
ผมอยากทราบว่าจะเช็คว่าต้องเป็นตัวอักษรเท่านั้นครับ
|
 |
 |
 |
 |
Date :
2009-11-10 12:05:02 |
By :
นฤคม |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0
if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
} }
//-->
</script>
ส่วนคำสั่ง textbox
<input name="textfield" type="text" id="textfield" onblur="MM_validateForm('textfield','','NisNum');return document.MM_returnValue" />
อันนี้ได้แน่ๆครับ
|
 |
 |
 |
 |
Date :
2009-11-11 11:48:39 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ได้ดีครับผม
ของท่าน PlaKriM นะ
|
 |
 |
 |
 |
Date :
2011-07-08 19:30:33 |
By :
แก่น |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (JavaScript)
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<form name="frmMain" action="" method="post">
<script language="JavaScript">
function chkNumber(ele)
{
var vchar = String.fromCharCode(event.keyCode);
if ((vchar<'0' || vchar>'9') && (vchar != '.')) return false;
ele.onKeyPress=vchar;
}
</script>
Input Number <input type="text" name="txtNumber" value="" OnKeyPress="return chkNumber(this)">
<input type="submit" name="btnSubmit" value="Submit">
</form>
</body>
</html>
Go to : JavaScript Check Validate Number only input
|
 |
 |
 |
 |
Date :
2011-07-10 08:54:41 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แนะนำอย่าใช้ javascript เพราะอาจโดนผู้ใช้ปิด javascript โดยไม่รู้ตัว
|
 |
 |
 |
 |
Date :
2017-05-12 09:31:39 |
By :
โต้ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|