ExtJs4.2 登陆界面(点击验证码自动刷新,label实现click事件)

ExtJs4.2 登陆界面(点击验证码自动刷新,label实现click事件)

转载请注明:http://blog.csdn.net/qiuzhping/article/details/42596339

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
<head>
	<title>Welcome</title>
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/main.css" />
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/icon.css" />
	<script type="text/javascript" src="<%=request.getContextPath()%>/Ext/include-ext.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/common/Cookie.js"></script>
</head>
<script type="text/javascript">

	function randomColor(){
		  var colorStr=Math.floor(Math.random()*0xFFFFFF).toString(16).toUpperCase();
		  return"#"+"000000".substring(0,6-colorStr)+colorStr;
	}
	
	function createCode() {
		code = "";
		var codeLength = 4;  
		var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C',
				'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
				'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');//随机数  
		for (var i = 0; i < codeLength; i++) {  
			var index = Math.floor(Math.random() * 36);  
			code += random[index];
		}
		cp.set("hiddenValidationCode",code);
		return code;
	}
	Ext.ux.ValidationCode = Ext.extend(Ext.Panel, {
		initComponent : function() {
			Ext.apply(this, {
				items : [ {
					xtype : 'label',
					id : 'validationCode',
					text:createCode(),
					style:'font-size: 20px; color: #CC0000;background-color:#6699FF',
					listeners : {
						render : function() {
							Ext.fly(this.el).on('click', function(e, t) {
								Ext.getCmp("validationCode").setText(createCode());
							});
						},
						scope : this
					}
				} ]
			});
			Ext.ux.ValidationCode.superclass.initComponent.call(this);
		}
	});

	Ext.onReady(function() {
		Ext.QuickTips.init();
		var loginForm = new Ext.form.Panel({
			listeners : {
				specialkey : function(field, e) {
					if (e.getKey() == Ext.EventObject.ENTER) {
						login();
					}
				}
			},
			fieldDefaults : {
				msgTarget : 'side',
				labelWidth : 100
			},
			defaultType : 'textfield',
			defaults : {  
				margin : '10 10 10 45'
			},
			items : [ {
				fieldLabel : 'Login Name:',
				name : 'user.username',
				id:'username',
				emptyText : 'Please enter your login name',
				allowBlank : false,
				blankText : 'Login name cannot be empty'
			}, {
				name : 'user.password',
				fieldLabel : 'Password:',
				id:'userpassword',
				inputType : 'password',
				emptyText : 'Please enter your password',
				allowBlank : false,
				blankText : 'Password can not be empty'
			}, {
				xtype : 'container',
				layout : 'hbox',
				fieldDefaults : {
					labelAlign : 'side'
				},
				items : [ {
	                xtype:'textfield',
	                editable : false,
	                fieldLabel: 'validationCode',
	                value:'validationCode',
	                id:'operatorId',
	                name: 'operatorId',
	                hideable:false,
					hidden:true
	             },{
					name : 'checkValidationCode',
					id : 'checkValidationCode',
					width : 180,
					xtype : 'textfield',
					fieldLabel : 'Validation Code:',
					listeners : {
				       change : function(field,newValue,oldValue){
				          if(newValue.length > 4){
				        	  Ext.Msg.alert('Information','Validation code length must be equals 4.');
				        	  Ext.getCmp("checkValidationCode").setValue("");
				          }
				       }
					}
				}, new Ext.ux.ValidationCode({
					renderTo : document.body
				})]
			} ],
			buttonAlign : 'center',
			buttons : [ {
				text : 'Submit',
				handler : login
			}, {
				text : 'Reset',
				handler : reset
			} ],
			listeners : {
				render : function(input) {
					new Ext.KeyMap(input.getEl(), [ {
						key : Ext.EventObject.ENTER,
						fn : function() {
							login();
						}
					} ]);
				}
			}
		})

		
		var win = Ext.create('Ext.window.Window', {
			title : 'Login Form',
			collapsible : false,
			animCollapse : false,
			maximizable : false,
			closable : false,
			draggable : false,
			width : 350,
			items : loginForm
		});
		win.show();
		function checkUserInfo(){
			Ext.getCmp("userpassword").setValue(Ext.util.Format.trim(Ext.getCmp("userpassword").getValue()));
			Ext.getCmp("username").setValue(Ext.util.Format.trim(Ext.getCmp("username").getValue()));
			if(Ext.getCmp("userpassword").getValue() == '' || Ext.getCmp("username").getValue() == ''){
				Ext.Msg.alert("Information",'Invalid input. Please check form entries.');
				return false;
			}
			return true;
		}
		function login() {
			if(!checkUserInfo()){
				return;
			}
			var code1 = cp.get("hiddenValidationCode");
			var code2 = Ext.getCmp("checkValidationCode").getValue();
			if(code1.toLowerCase() != code2.toLowerCase()){
			   Ext.Msg.alert('Information','Please enter validation code');
       		   return;
       	  	}
			if (loginForm.isValid()) {
				loginForm.getForm().submit({
					clientValidation : true,
					url : '/report/Login!login.action',
					method : 'post',
					success : function(form, action) {
						Ext.Msg.alert("Information","Login success");
						cp.set("username",Ext.getCmp("username").getValue());
						document.location = "index.jsp";
					},
					failure : function(form, action) {
						Ext.Msg.alert("Information",'Invalid login name or password!');
					}
				})
			}
		}
		function reset() {
			loginForm.form.reset();
		}
	});
</script>
</html>

效果图:


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值