話不多說,請看代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <link rel="stylesheet" type="text/css" href="" /> <style type="text/css"></style> <script type="text/javascript" src="jquery-3.0.0.js"></script> </head> <body> <div style="width:300px;height:100px;border:1px solid #E5E5E5;margin:0 auto;margin-top:30px;"> <form action="" method="post">  <span>用戶名:</span><input type="text" name="username" class="username" value="" />  <input type="submit" value="提交" class="tijiao"> </form>  <script type="text/javascript">  $(document).ready(function(){    $(".tijiao").click(function(){       //var name = $.trim($(".username").val());       var name= $('input[name=username]').val().replace(/(^/s*)|(/s*$)/g,"");       if(name==""){        alert("用戶名不能為空");       }     });  });  /*    /(^/s*)|(/s*$)/g 包含以空格、回車符等字符開頭或者空格、回車符等字符結(jié)尾的字符串,可過濾出所有空格、回車符的字符    /g意思就是:global可選標(biāo)志,帶這個(gè)標(biāo)志表示替換將針對行中每個(gè)匹配的串進(jìn)行,否則則只替換行中第一個(gè)匹配串。如:we.fdffddfwe.加上/g后,則2個(gè)we都會出來;    /s 空白字符    replace() 方法用于在字符串中用一些字符替換另一些字符,或替換一個(gè)與正則表達(dá)式匹配的子串。    stringObject.replace(regexp/substr,replacement)  */  </script> <div> </body></html>以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!
新聞熱點(diǎn)
疑難解答