iPhone Project RegisterPage control the TextField and description
先把所有需要的TextField的tag给声明。然后在头文件里声明event。注意,在xib里连接event的时候,右击FilesOwner,找event,连到editing changed,保存。
头文件:-(IBAction)textViewDidChange:(UITextView *)textView;
1.电话号码TextField 控制 只能输入数字,并为10-11个字代码。
-(IBAction)textViewDidChange:(UITextView *)textView{
if(textView.tag == 2){
NSString *laxString = @"[1-9]\d*|0";//正则表达式 数字
NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",laxString];//生成一个NSPredicate object,laxString style。。。
if(textView.text.length>11){
textView.text = [textView.text substringToIndex:11];//长度超过11,就输出到第11.
}else{
if(textView.text.length!=0){ //判断最后一个字母是数字还是别的,如果不是数字,输出最后一个字母前面的就行。
if(![number.Predicate evaluateWithObject:[textView.text substringFromIndex:textView.text.length -1]]){
textView.text=[textView.text substringToIndex:textView.text.length-1];
}
}
}
}
}
}
//补充说明 substring**Index:(copy 从http://www.codesnipr.com/snippet/384/Objective-C-Substring-using-substringToIndex-and-substringFromIndex-)
NSString *quote = @"Life is a succession of moments. To live each one is to succeed.";
NSString *life = [quote substringToIndex:4];
NSString *first_part = [quote substringToIndex:32];
NSString *second_part = [quote substringFromIndex:33];
NSLog(@"%@",life);
NSLog(@"%@",first_part);
NSLog(@"%@",second_part);
输出结果 Output:
Life
Life is a succession of moments.
To live each one is to succeed.
2.判断TextField 里的邮箱是否合法
先判断是否空,然后用正则表达式判断。(txt_email 是自己TextField对应的名字。)
if ([txt_email.text isEqualToString:@“”])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Plz input ur email." delegate:self cancelButtonTitle:@@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}else
{
NSString *laxString = @.+@.+\.[A-Za-z]{2}[A-za-z]*;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MACHES %@",laxString];
if(![emailTest evaluateWithObject:txt_email.text])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Plz input ur email correctly" delegate:self cancelButtonTitle:@@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
}
3.TextField 内容的长度限制。(比如4-12内:至少4-》用alert限制,超过12个,就只输出前12位。)
//每次输入一个字母的时候,call :-(IBAction)textViewDidChange:(UITextView *)textView;这个event,所以能每个字符每个字符检查,超过12了就输出到12就行。
if(textView.tag==1)
{
if(textView.text.length>12)
{
textView.text = [textView.text substringToIndex:12];
}
}
最后提交时,alert就行。
4.判断TextField是不是空
if ([txt_email.text isEqualToString:@“”])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Plz input ur email." delegate:self cancelButtonTitle:@@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}