PDA

View Full Version : سوال: ورود فقط عدد در تکس



samsami
یک شنبه 19 آذر 1391, 21:27 عصر
با سلام خدمت دوستان

من یک text field در html دارم و می خوام فقط عدد قبول کنه یعنی کسی نتونه حروف انگلیسی و فارسی تایپ کنه و فقط بتونه عدد وارد کنه ...

ممنون میشم از پاسختون...

pani.khoram
یک شنبه 19 آذر 1391, 22:23 عصر
سلام
<html>

<script type="text/javascript">
var numString; //this keeps track of the valid string

//check the input after a key press
function checkinput(e){

//this gets the value of the key pressed
var aplhaNumericCode=e.keyCode? e.keyCode : e.charCode

//if the key is a number then add it to the number string. (This is not actual js code.)
if(aplhaNumericCode>=65 //*a*/ || aplhaNumericCode>=90//*z*/)
putnum()
else
alert (keyPressed)
}
//enter the valid string in the text box (This is not actual js code.)
function putnum(){
var num = document.getElementByID("number")
num.text = numString
}
</script>
<body>
<form>
<input id ="number" type="text" onkeyup="checkinput(event);" />
</form>
</body>
موفق باشید

samsami
سه شنبه 21 آذر 1391, 08:01 صبح
pani.khoram (http://barnamenevis.org/member.php?272894-pani.khoram)
http://barnamenevis.org/images/statusicon/user-offline.png خواهشا شما پاسخ ندید اول کدتون رو تست کنید سپس پاسخ دهید ...
کدتون اشتباه

2undercover
سه شنبه 21 آذر 1391, 13:57 عصر
اصلا یک نوع input در html وجود داره که فقط مقدار عددی قبول می کنه به این صورت:
<input type="number" name="name" size="size">

Sni[er
شنبه 25 آذر 1391, 15:47 عصر
<!doctype html>
<html>
<head>
<script src="jquery-1.8.2.min.js"></script>
<script>
$(function(){
var count = 0;
$('#txt').bind('keypress keydown',function(e){
var key;
if (e.type == 'keydown' || e.type == 'keypress') key = e.keyCode || e.which;
if( key >= 48 && key <= 57 || key >= 96 && key <= 105 )
return true;
return false;
});
});
</script>
</head>
<body>
<textarea id="txt"></textarea>
<br/>
<span id="count"></span>
</body>
</html>