判断邮箱格式是否正确
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. emailTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)]; emailTextField.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:emailTextField]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(100, 170, 150, 50); [button setTitle:@"注册" forState:UIControlStateNormal]; [button addTarget:self action:@selector(registor:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)registor:(UIButton *)button { if([self isValidateEmail:emailTextField.text]){ NSLog(@"邮箱格式正确"); }else { NSLog(@"不是"); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"请输入正确的邮箱" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil nil]; [alert show]; } }
邮箱格式要求是:
必须包含一个并且只有一个符号@
第一个字符不能是'@'或者'.'
不允许出现'@.'或者'.@'或者'-@'或者'@-'
结尾不得是字符'@'或者'.'
字符@后面只能是'A-Za-z0-9'
<script> function checkMail(str){ var strReg=""; var r; var strText=document.all(str).value; //strReg=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i; strReg=/^\w+((-\w+)|(\.\w+))*\@{1}\w+\.{1}\w{2,4}(\.{0,1}\w{2}){0,1}/ig; r=strText.search(strReg); if(r==-1) { alert("邮箱格式错误!"); document.all(str).focus(); } }//by www.jbxue.com 脚本学堂 </script><input name="email" type="text" class="input" id="email" onChange="checkMail('email')"> <script language=javascript> <!-- function checkform() { if(addform.yx.value != '') { if(!Isyx(addform.yx.value)){ alert("请输入正确的邮箱地址!") addform.yx.focus(); return false; } } } function Isyx(yx){ var reyx= /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;//使用正则 return(reyx.test(yx)); } //--> </script>