• 表单验证必填项


    在表单验证中,会有一些必填项,如手机号,密码,昵称,验证码等,如果某项为空,或格式不正确表单将无法提交。在此我只做了手机号和密码的验证,其他的与此类此

    html代码:

                <form action="" onsubmit="return check_sub()">
    			<p class="tel">
    				<label for="tel">手机</label>
    				<input type="text" id="tel" onblur="yt_tel()">
    				<span>手机格式不正确</span>
    			</p>
    			<p class="psd">
    				<label for="psd">密码</label>
    				<input type="password" id="psd" onblur="yt_psd()">
    				<span>密码格式不正确</span>
    			</p>
    			<p>
    				<input type="submit" id="sub">
    			</p>
    
    		</form>        
    

      js代码

    	<script type="text/javascript">
    		function check_sub(){
    			var check=yt_tel()&&yt_psd();
    			if(!check){
    				return false;
    				console.log('false');
    			}else{
    				return true;
    			}
    		}
    		//验证手机号
    		var regTel=/(1[3-9]\d{9}$)/;
    		
    		function yt_tel(){
    			var tel=$('#tel').val();
    			if (!regTel.test(tel)) {
    				$('.tel span').show();
    				return false;
    			}else{
    				return true;
    
    			}
    		}
    		//验证密码
    		var regPsd=/^[_a-zA_Z0-9]{6,12}/;
    		function yt_psd(){
    			var psd=$('#psd').val();
    
    			if (!regPsd.test(psd)) {
    				$('.psd span').show();
    				return false;
    			}else{
    			return true;
    			}
    		}
    
    	$('#tel').focus(function(){
    		$('.tel span').hide();
    	});
    	$('#psd').focus(function(){
    		$('.psd span').hide();
    	});
    		
    	</script>
    

      

  • 相关阅读:
    JSTL笔记(胖先生版)
    EL表达式(胖先生版)
    包装类-Character
    String定义与方法
    冒泡排序(大熊版)
    tomcat Manger App
    第一天
    剑指offer:面试题5、从尾到头打印链表
    剑指offer:面试题4、替换空格
    剑指offer:面试题3、二维数组中的查找
  • 原文地址:https://www.cnblogs.com/lfhy/p/6782367.html
Copyright © 2020-2023  润新知