IOS TextField limit of input amount, 9 digits before decimal point, two digits behind

Source: Internet
Author: User

IOS TextField Enter the amount of the limit, 9 digits before the decimal point, two digits later, if no decimal point, the maximum number of digits is 9 bits, plus the decimal point, the maximum number of digits is 12 bits, the maximum number of digits can be deleted

-(BOOL) TextField: (uitextfield *) TextField shouldchangecharactersinrange: (nsrange) range Replacementstring: (nsstring *) string {

//Decide whether to enter the content, or the user clicked the Keyboard delete button

If (![ String isequaltostring:@ ""]) {

nscharacterset *cs;

if ([TextField isequal:counttextfield]) {

//The position of the decimal point in the string the first number starts at the 0 position

nsinteger dotlocation = [TextField. Text rangeofstring:@ "." ]. Location ;

//Determine if there are any decimal points in the string, and it is not the first digit

//Nsnotfound indicates that a request operation is something or item is not found, or does not exist

//Range.location represents the position of the current input in the entire string, where the position number starts at 0

if (dotlocation = = nsnotfound && range. Location! = 0) {

//Fetch contains only the content contained in "Mydotnumbers" and the remainder is removed

/*

The function of [Nscharacterset charactersetwithcharactersinstring:mydotnumbers] is to remove everything contained in the "Mydotnumbers" as long as the contents of the string contain the " Mydotnumbers "Part of the content will be abandoned

Adding Invertedset at the end of the above method will reverse the action, taking only the same characters as the contents of "Mydotnumbers"

*/

CS = [[nscharacterset charactersetwithcharactersinstring:numberswithdot] invertedset ];

if (range. Location >= 9) {

NSLog(@ "The amount of a single sum cannot exceed billion");

if ([string isequaltostring:@ ".") ] && range. Location = = 9) {

return YES;

}

return NO;

}

}Else {

CS = [[nscharacterset charactersetwithcharactersinstring:numberswithoutdot] Invertedset];

}

separate arrays by CS, array by @ "" to isolate strings

nsstring *filtered = [[String Componentsseparatedbycharactersinset: cs] componentsjoinedbystring :@""];

BOOL basictest = [string isequaltostring: filtered];

if (!basictest) {

NSLog(@ "Only enter numbers and decimal points");

return NO;

}

if (dotlocation ! = Nsnotfound && range. Location > dotlocation + 2) {

NSLog(@ "up to two digits after the decimal point");

return NO;

}

if (TextField. Text. Length > one) {

return NO;

}

}

}

return YES;

}

IOS TextField limit of input amount, 9 digits before decimal point, two digits behind

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.