2017-09-13 119 views
0

我正在尋找一些幫助驗證與聯繫表格7名稱字段。我已經收到了大量的垃圾郵件,名稱字段包含數字'59ab ...'。尋找一種方法來驗證除數字以外的任何其他內容。我嘗試過使用jQuery,但沒有成功。驗證姓名字段與聯繫表7

嘗試此更早但不成功。

$(".your-name input").change(function() { 
var num = $(this).text(); 
if ($.isNumeric(num)) { 
    $(this).attr('class', 'numb'); 
} else { 
    $(this).attr('class', 'noNumb'); 
} 

});

+0

改變'$(本)的.text()''到$(本).VAL()' –

回答

0

只需將$(this).text()更改爲$(this).val()即可解決您的問題。檢查下面的代碼片段以供參考。

$(".myinput").change(function() { 
 
    var num = $(this).val(); 
 
    if ($.isNumeric(num)) { 
 
    $(this).attr('class', 'numb'); 
 
    } else { 
 
    $(this).attr('class', 'noNumb'); 
 
    } 
 
});
.numb { 
 
    border-color: green; 
 
} 
 

 
.noNumb { 
 
    border-color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" class="myinput">

更新:檢查下面的代碼片段。這裏我使用了正則表達式來檢測輸入中是否有數字。

$(".myinput").change(function() { 
 
    var num = $(this).val().replace(/[^0-9]/gi, ''); 
 
    if ($.isNumeric(num)) { 
 
    $(this).attr('class', 'numb'); 
 
    } else { 
 
    $(this).attr('class', 'noNumb'); 
 
    } 
 
});
.numb { 
 
    border-color: green; 
 
} 
 

 
.noNumb { 
 
    border-color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" class="myinput">

+0

感謝喜的片段中,我看到的輸入值的工作,只是一個數字,但我需要能夠檢測字符串中的任何數字以及作爲引起垃圾郵件的名稱的字母。所以理論上,如果一個名字被輸入爲'59ab',那麼切換這些類?你的意思是 –

+0

,我們需要檢查它是否包含任何輸入數字? –

+0

@ShaunShirleyPhillips檢查更新的答案。 –