Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:17:22+00:00 2026-06-03T03:17:22+00:00

I have a quick question that I can’t find anywhere. I am limiting my

  • 0

I have a quick question that I can’t find anywhere. I am limiting my text field to just numbers, decimals, and the negative sign. But in doing so, I turned off the return key. Anyone know the unichar number for iOS’s return key? I thought it was thirteen but that is not working.

 - (BOOL)textField:(UITextField *)textField 
 shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {
     NSUInteger lengthOfString = string.length;
for (NSInteger loopIndex = 0;  loopIndex < lengthOfString; loopIndex++)
     {
         unichar character = [string characterAtIndex:loopIndex]; 
         if (character < 45) return NO; //45 - 57 we want
         if (character > 57) return NO; // -./0123456789
         if (character == 47) return NO; //47 we dont want, it is 
 //             if (character  == 36) return YES
     }
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 5) ? NO : YES;
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:17:24+00:00Added an answer on June 3, 2026 at 3:17 am

    You might have some other reason to inspect the input. (I think comparing to ‘\n’ works), but this might be even better:

    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    

    Edit

    A more thorough treatment of handling floating point input might look like this. The key ideas are to build a candidate string with the proposed replacement, then test the whole string for syntactic validity. This way you can handle the user pasting in new text at some arbitrary position without touching the code. Regex is just a compact/quick way to specify syntax, you can put your own method in the validator.

    (I guessed that you might want to handle a minus sign as not literally part of the input, but rather an indicator that a negative number is desired. Also assumed that the length limitation is for significant digits, not simply total chars)

    // answers true for a valid floating point literal, notwithstanding length
    - (BOOL)isValidFloat:(NSString *)string {
    
        // optional sign, followed by zero or more digits, followed by decimal, followed by zero or more digits
        NSString *regex = @"([-+]?[0-9]*\\.?[0-9]*)";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [predicate evaluateWithObject:string];
    }
    
    #define kMAX_DIGITS  5
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        NSString *currentText = textField.text;
        NSString *candidateText = [currentText stringByReplacingCharactersInRange:range withString:string];
    
        // accept a minus sign at any time. Place it in front if it's not already there
        if (string.length == 1 && [string characterAtIndex:0]=='-') {
            if ([currentText rangeOfString:string].location == NSNotFound) {
                textField.text = [string stringByAppendingString:currentText];
                // since we just inserted the minus, answer no so it won't be inserted twice
                return NO;
            }
        }
    
        // the decimal and minus sign don't count against the length constraint
        NSInteger decimalFudge = ([candidateText rangeOfString:@"."].location != NSNotFound)? 1 : 0;
        NSInteger minusFudge = (candidateText.length > 0 && [candidateText characterAtIndex:0]=='-')? 1 : 0;
        NSInteger maxLength = kMAX_DIGITS + decimalFudge + minusFudge;
    
        return (candidateText.length <= maxLength) && [self isValidFloat:candidateText];
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
        return YES;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A quick question, that I can't seem to find the answer to anywhere to
Quick question that I can't seem to find anywhere; at least not a clear
Just a quick simple question that I can not seem to find an answer
I have a quick question here. I know that the cakePHP find('first') function returns
just a question that needs a quick answer, I have a Action, lets say,
Just a quick newbie question here. I have a method that calculates a value
Quick regex question (since i am horrible at it) I have a field that
I've just got a quick question that if you have time would be great
I have a quick and hopefully simple question that I hope someone can help
I have a quick question that I can't figure out. I've tried searching Google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.