JS控制input 文本框只容許輸入漢字

 

onblur="value=value.replace(/[^/u4E00-/u9FA5]/g,'')"
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/u4E00-/u9FA5]/g,''))">
<input type="text" size="30" maxlength="11" value="" onblur="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" 
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">

onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"ui

onbeforepaste 意思是在用戶執行粘貼動做以前。
clipboardData.setData('text', xxx)  是把xxx的內容複製到剪貼板

clipboardData.getData('text')  是讀出當前剪貼板裏的內容,

.replace(/[^\d]/g,'')  是正則替換,把裏面除了數字之外的字符所有都去掉,

整個語句的功能是,每當用戶執行粘貼操做前,先取出剪貼板的內容字符串,刪除不是數字的字符,只保留數字,而後再粘貼,而不是直接粘貼spa

by 小鬼PSercode

相關文章
相關標籤/搜索