js驗證輸入格式常用到的一些驗證、js匹配(持續補充)

url

缺點不支持中文URL:

(http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?

實例:

var regexp = new RegExp("(http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&amp;*+?:_/=<>]*)?", "gi");
var urls = textbox.value.match(regexp) || [];//textbox爲文本框
console.log(urls);




小時

str = str.match(/^(?:0?[0-9]|1[0-9]|2[0-4])$/);

if (str == null) alert("小時格式不正確");


分鐘

str = str.match(/^[0-5]?[0-9]$/);

if (str == null) alert("分鐘格式不正確");


數字

        this.value = this.value.replace(/[^\d]/g,"");  //清除“數字”和“.”以外的字符 
        this.value = this.value.replace(/^\./g,"");  //驗證第一個字符是數字而不是. 
        this.value = this.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

數字和小數點

        this.value = this.value.replace(/[^\d.]/g,"");  //清除“數字”和“.”以外的字符 
        this.value = this.value.replace(/^\./g,"");  //驗證第一個字符是數字而不是. 
        this.value = this.value.replace(/\.{2,}/g,"."); //只保留第一個. 清除多餘的. 
        this.value = this.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

發佈了15 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章