js判断邮箱格式是否正确 的几个例子

判断邮箱格式是否正确

 

- (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> 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值