我正在尝试使用以下正则表达式验证电子邮件地址字段
+ (BOOL) isValidEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
但它在验证以下案件时失败了。
i)test@-gmail.com
ii)test..test @ gmail.com
iii).test @ gmail.com
iv)测试。@ gmail.com
请建议我在考虑这些情况时需要做哪些修改。
答案 0 :(得分:0)
试试这个。
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ToPostTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->text('body');
$table->timestamps();
$table->timestamp('published_at');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts');
}
}
并检查电子邮件是否有效。
-(BOOL)emailValidation:(NSString *)str{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL myStringMatchesRegEx=[emailTest evaluateWithObject:str];
if (myStringMatchesRegEx == 0) {
return FALSE;
}
return TRUE;
}