2013年9月4日 星期三

HTML text 輸入限制

限輸入數字

<input type="text" onKeyUp="this.value=this.value.replace(/\D/g,'')" />
this.value=this.value.replace(/[^0-9]/g,'')
.replace(/[^\d]/g,'')



允許數字 . (點)
this.value=this.value.replace(/[^0-9.]/g,'')





[時間格式 Y-m-d H:i:s ]  允許數字及 : (冒號) 及  -  (減號)    
this.value=this.value.replace(/[^\d\s:-]/g,'')
this.value.replace(/[^\d&:&-]/g,'')
[電話格式 ]
this.value=this.value.replace(/[^\d&-]/g,'')






允許中文
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">


<input type="text" style="ime-mode:disabled">








清空文字
onfocus="if(this.value == '請輸入關鍵字'){  this.value=''; };"








去空白
onblur="this.value=this.value.trim();"






ps.  PHP   trim( String );
       str_replace(" ", "", String);    //去除(取代)全型空白











參考資料
http://blog.kkbruce.net/2012/02/javascript-regular-expression.html#.Um8YaBBi1Mc
http://blog.sina.com.cn/s/blog_9d37865901016ch3.html
http://blog.fend.cn/archives/54.html
http://blog.xuite.net/zong36/home/63327602-PHP-%E6%AD%A3%E5%89%87%E8%A1%A8%E7%A4%BA%E6%B3%95%28%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%A4%BA%E6%B3%95%29
http://www.webpage.idv.tw/maillist/maillist4/pro/05/regular1.htm    正規表示法圖說
http://www.eion.com.tw/Blogger/?Pid=1047
http://calos-tw.blogspot.tw/2007/12/javascript.html

沒有留言:

張貼留言