jquery replace方法去空格
2017-05-08 11:05
585 查看
<!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 包含以空格、回车符等字符开头或者空格、回车符等字符结尾的字符串,可过滤出所有空格、回车符的字符 /g意思就是:global可选标志,带这个标志表示替换将针对行中每个匹配的串进行,否则则只替换行中第一个匹配串。如:we.fdffddfwe.加上/g后,则2个we都会出来; \s 空白字符 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。 stringObject.replace(regexp/substr,replacement) */ </script> <div> </body> </html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
您可能感兴趣的文章:
相关文章推荐
- jquery replace方法去空格
- ext:TextField 不能判断是否输入的是空格解决方法!!!
- 数据库中去掉列中空格的方法!
- 在WPS或Excel中多列合并成一列合并后中间加空格或-或逗号的写法,可参照以下方法,wps与excel操作与用法都相同
- c#中去掉字符串空格方法
- Unix, Linux, Mac OS 命令行中带有空格的处理方法
- c++ String去除头尾空格的方法 trim实现
- Android开发中请求URL中参数含有中文和空格的解决方法
- python清除字符串中间空格的方法
- 请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成
- js去掉字符串前后空格的五种方法
- JavaScript去除空格的三种方法 (trim)
- c#去掉字符串空格的的方法
- python实现指定字符串补全空格的方法
- js中几种去掉字串左右空格的方法
- linux 处理文件名中带空格的方法
- 如何用正则将多个空格看成一个空格结合spllit()方法将文本数据入库
- python清除字符串前后空格函数的方法
- CmD空格转义的三种方法,总有一种会解决问题
- mysql命令语句来去除掉字段中空格字符的方法