Usually in the edit box to enter some number of goods, the amount of money, we will do some input judgment limit. For example, you cannot enter illegal characters, only numbers, and so on. Some of the recently encountered input restriction requirements are shown below:
1, the input decimal place cannot exceed two bits;
2, cannot repeat the input decimal point;
3, can not enter characters other than the number (but does not include backspace key, minus key);
4, the limit length is minimum 1, the maximum is 11.
In order to achieve the above requirements, mainly rely on the following uitextfielddelegate a protocol method to achieve:
-(BOOL)TextField:(Uitextfield*)TextFieldShouldchangecharactersinrange: (nsrange) Range replacementstring: (nsstring*)string{function: PutTextFieldA string with a range in the position is replaced bystringstring; This function isTextFieldCalled when the content is modified; the return value: YES, indicating that the modification takes effect;TextFieldThe content is not changed. Parameter description:TextField: The Uitextfield control that responds to the Uitextfielddelegate protocol. The string selected by the cursor in the Range:uitextfield control, which is the replaced string; Range.length is0Range.location is inserted at positionstring。string: replace string;string. length is0Delete is clicked.DeleteKey.
All of the following code needs to be written in the function.
1. Input decimal digits cannot exceed two digits
/** * Check floating-point decimal digits and check the legitimacy of the decimal point * * @param Range Floating-point total length * @param dotplaces decimal Place * @param Dec iMAL floating point * @param String Current input value * * @return YES: No decimal point or limit two-bit floating-point number no */+ (BOOL) Checkdecimaldotplaces: (nsrange) Range dotplaces: (NSNumber *) dotplaces decimal: (NSString *) decimal{BOOL Bhavedot =false;if(Dotplaces.integervalue <0) {dotplaces = @0; }if([Decimal rangeofstring:@"."].location = = nsnotfound) {Bhavedot =false; }Else{Bhavedot =true; }if(Bhavedot) {//Determine the number of decimal placesNsrange ran = [Decimal rangeofstring:@"."];if(Range.location-ran.location > Dotplaces.integervalue) {returnNO; } }returnYES;}
2. Cannot enter the decimal point repeatedly
//不能再输入小数点if ([string rangeOfString:@"."].locationNSNotFound && [textField.text rangeOfString:@"."].locationNSNotFound) {returnNO;}
3. You cannot enter characters other than numbers (but not the backspace key, minus key)
//不能输入字符串if ([string rangeOfString:@"."].locationNSNotFound) { NSRange range = [string rangeOfString:@"^[0-9-]+$" options:NSRegularExpressionSearch]; if (range.locationNSNotFound) { returnNO; }}//删除delete键if ([string isEqualToString:@""]) { returnYES;}
4, the limit length is minimum 1, the maximum is 11 bit
NSString *pwdRegex = @"[0-9]{1,10}";NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pwdRegex];if ([predicate evaluateWithObject:textField.text] || ![textField.text notEmpty]) { returnYES;}else{ returnNO;}
In addition, the judgment of the string can be chosen in the NSString classification extension, such as nsstring+extension.
The total code is as follows:
- (BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (Nsrange) Range Replacementstring: (NSString*) string{//cannot enter decimal point again if([String rangeofstring:@"."]. location!=Nsnotfound&& [NSStringIshavedot:textfield. Text]) {return NO; }//delete if([String isequaltostring:@""]) {return YES; }//Cannot enter string if([String rangeofstring:@"."]. location==Nsnotfound) {NsrangeRange = [string rangeofstring:@"^[0-9]+$"Options:nsregularexpressionsearch];if(Range. location==Nsnotfound) {return NO; } }//Limit length is minimum 1, maximum is 11 bits NSString*pwdregex = @"[0-9]{1,10}"; Nspredicate *predicate = [Nspredicate predicatewithformat:@"Self matches%@", Pwdregex];if([Predicate Evaluatewithobject:textfield. Text] || ! [TextField. TextNotempty]) {return YES; }Else{return NO; }//Limit the length of the decimal place to not exceed 2 if(! [NSStringCheckdecimaldotplaces:range dotplaces:zhjfloatdotplaces Decimal:textfield. Text]) {return NO; }return YES;}
iOS Learning record-Uitextfield the limit for entering floating-point numbers