正則表示式 驗證輸入框內的字元含是否含有[email protected]#$%^&*字元
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"> </script> </head> <body> <p>題目:請校驗輸入框內的字元含是否含有[email protected]#$%^&*字元</p> 請輸入題目規定的字元:<input type="text" id="in" /> <input type="button" id="btn" value="click"/> <script> $(function () { var truePwd = /[\!\@\#\$\%\^\&\*]/; $("#btn").click(function () { console.log( $("#in").val()); if(truePwd.test( $("#in").val())){ alert("含有字元: "+truePwd.exec( $("#in").val())); }else{ alert("匹配失敗"); } }) }) </script> </body> </html>
運用方法:
Test():檢測會否存在,返回值為true/false;
exec():檢索指定的值,返回值是被找到的值,如果沒有找到,返回null;