JavaScript禁止输入法,检验是否是合法的ascoii码,禁止粘贴不合法的值,增加和消除千分位
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
< head >
< title > JavaScript </ title >
< script type ="text/javascript" >
style = " ime-mode:disabled " // 禁止输入法
function numKeyDown(){
var k = window.event.keyCode;
// 判断是否是合法的ascii
return isValidNum(k);
}
function isValidNum(k){
if ((k == 0 ) || (k == 13 ) || (k == 46 ) || (k == 8 )){
return true ;
}
else {
return false ;
}
}
function numPaste(){
var data = window.clipboardData.getData( " Text " );
for ( var i = 0 ;i < data.length;i ++ ){
var ascii = data.charCodeAt(i);
if ( ! isValidNum(ascii)){
return false ;
}
}
}
function commafy(){
}
</ script >
</ head >
< body >
禁止输入法: < input type ="text" style ="ime-mode:disabled" />
判断是否是合法的ascii < input type ="text" onkeydown ="return numKeyDown()" />
禁止粘贴不合法的值 < input type ="text" onpaste ="return numPaste()" />
增加、消除千分位 < input type ="text" onfocus ="this.style.textAlign='left';this.value=this.value.replace(/,/g,'');" onblur ="this.style.textAlign='right';this.value=commafy(this.value)" />
</ body >
</ html >
< head >
< title > JavaScript </ title >
< script type ="text/javascript" >
style = " ime-mode:disabled " // 禁止输入法
function numKeyDown(){
var k = window.event.keyCode;
// 判断是否是合法的ascii
return isValidNum(k);
}
function isValidNum(k){
if ((k == 0 ) || (k == 13 ) || (k == 46 ) || (k == 8 )){
return true ;
}
else {
return false ;
}
}
function numPaste(){
var data = window.clipboardData.getData( " Text " );
for ( var i = 0 ;i < data.length;i ++ ){
var ascii = data.charCodeAt(i);
if ( ! isValidNum(ascii)){
return false ;
}
}
}
function commafy(){
}
</ script >
</ head >
< body >
禁止输入法: < input type ="text" style ="ime-mode:disabled" />
判断是否是合法的ascii < input type ="text" onkeydown ="return numKeyDown()" />
禁止粘贴不合法的值 < input type ="text" onpaste ="return numPaste()" />
增加、消除千分位 < input type ="text" onfocus ="this.style.textAlign='left';this.value=this.value.replace(/,/g,'');" onblur ="this.style.textAlign='right';this.value=commafy(this.value)" />
</ body >
</ html >