javascript only input float/integer

<html>

<head>

<scriptsrc="jquery-1.11.3.min.js"></script>

<scriptlanguage="Javascript">

$(document).ready(function(){

$('.float-number').keypress(function(event){

varcode=event.which;

if(code==8//Backspace

||code==9//HorizontalTab

||code==27//ESC

||code==13//CarriageReturn

||code==45//Hyphen,minussign

||code==46//Dot

||((code==65||code==97)&&event.ctrlKey===true)//Ctrl+A

||((code==67||code==99)&&event.ctrlKey===true)//Ctrl+C

||((code==88||code==120)&&event.ctrlKey===true)//Ctrl+X

){

varvalue=$(this).val();

if(code==46&&value.indexOf('.')!=-1){

event.preventDefault();

}

if(code==45&&value.length>0){

event.preventDefault();

}

return;

}

if(event.which<48

||event.which>57){

event.preventDefault();

}

});

$('.int-number').keypress(function(event){

varcode=event.which;

if(code==8//Backspace

||code==9//HorizontalTab

||code==27//ESC

||code==13//CarriageReturn

||code==45//Hyphen,minussign

||((code==65||code==97)&&event.ctrlKey===true)//Ctrl+A

||((code==67||code==99)&&event.ctrlKey===true)//Ctrl+C

||((code==88||code==120)&&event.ctrlKey===true)//Ctrl+X

){

varvalue=$(this).val();

if(code==45&&value.length>0){

event.preventDefault();

}

return;

}

if(event.which<48

||event.which>57){

event.preventDefault();

}

});

});

</script>

</head>

<body>

<p/>

<p/>

Integer:<inputclass="int-number"type="Text"id="float_input"value=""><br>

<p/>

<p/>

Float:<inputclass="float-number"type="Text"id="float_input"value=""><br>

</body>

相关推荐