72247

How can I disable the decimal key on iOS keyboard

Question:

I want to disable the "." key on the number pad after detecting that the user already has entered in one decimal point into the textfield.

So the key is enabled until one decimal is detected in the textfield.

What is the best way of doing this?

EDIT: I've implemented the method below:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if([string isEqualToString:@"."]) { decimalCount++; } if(decimalCount > 1) { [string stringByReplacingOccurrencesOfString:@"." withString:@""]; } return YES; }

However, it's not replacing "." with "" when decimal count is greater than 1. What am I missing so that the user can still enter new digits but not decimal points??

Answer1:

You can't disable the key. Implement the shouldtextField:shouldChangeCharactersInRange:replacementString: delegate method to do whatever filtering you need.

This needs to be done anyway since a user could be using an external keyboard or attempt to paste text into the text field.

Answer2:

Sorry if my earlier answer mislead you. The logic was incorrect. This seems to be working for me now.

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { BOOL result = YES; NSString *targetString = @"."; if([textField.text rangeOfString:targetString].location != NSNotFound) { if([string isEqualToString:targetString]) { result = NO; } } return result; }

Hope it helps.

Recommend

  • Resource id #5: MySQL
  • value from MySQL needs to be refreshed
  • PIC - is there a value for RB7?
  • want to know ever time while pressing on keyboard back button during textfield editing ios
  • shouldChangeCharactersInRange call twice
  • Unable to build Android+Scala project using sbt+sbt-android plugin
  • How to make round cornered NSTableView rows?
  • CTFrameGetVisibleStringRange equivalent for iOS programming?
  • how to validate textfield that allows two digits after
  • Trouble Setting A Maximum Length For A Text Field
  • TTTAttributedLabel Delegate didSelectLinkWithURL is not getting called in iPhone
  • Allow only alpha numeric characters in UITextView
  • Login in a Webpage with Watin
  • Validate JavaFX form only after submission using ValidationSupport
  • How to programmatically select a UITextField for editing
  • PageSize not working on Grid in ExtJS
  • Adding .lib files in Microsoft Visual Studio 2010
  • grails controllers render errors
  • How to test method of JavaFX controller?
  • UITextField — observe changes to selectedTextRange?
  • for loop iteration in django
  • php: best way to validate POST
  • Client server multithread Socket
  • PHP file_exists() anomaly
  • Generate a unique string based on a pair of strings
  • Deleting a widget from QTableView
  • unrecognized selector with category NSMutableData
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Python cosine function precision [duplicate]
  • Build Successful but not running on simulator
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Excel's Macro-Recorder usage
  • How to handle AllServersUnavailable Exception
  • VBA Convert delimiter text file to Excel
  • how does django model after text[] in postgresql [duplicate]
  • Reading document lines to the user (python)