UITextField shouldChangeCharactersInRange - ios

I am implementing a textfield to input user passcode. I am trying to match the length of stored passcode with user entered passcode. When the length matches i try to validate passcode. If the passcode does not match or length gets greater than stored value then i try to clear the textfield (the else-if) but the last entered character still stays in text field.
Please propose what i am doing wrong or what is the proper way?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
enteredPasscode = [enteredPasscode stringByAppendingString:string];
if(enteredPasscode.length == [[self.passcodeFromDB stringValue] length])
{
[self performMatch:enteredPasscode];
}
else if(enteredPasscode.length > [[self.passcodeFromDB stringValue] length]){
self.passcodeField.text = #"";
textField.text = #"";
enteredPasscode = #"";
}
return YES;
}

you need to return NO; when you set everything to #"". I just checked and it works for me

Related

How to force the user to stop typing in UITextField but allow backspacing

I have a UITextField that my user uses to type out tags. I need to be able to at a very specific time - stop my user from continuing to type in the keyboard that is presented by default with UITextFields. However, I need my user to still be allowed to hit the backspace button on the iOS keyboard so that they can try and type a word that will fit.
A couple VERY important things to keep in mind:
The UITextField should not be frozen due to a maximum amount of characters, because I am determining when the textField should be frozen based off of UIViews that I am adding to the screen for the tags
The user still should be able to backspace
The keyboard should not be dismissed
The textField should not be disabled
I have tried setting the textField.enabled = NO, but once again, I need to still be able to use the textField, I just simply need to freeze the typing and force the user to backspace, not allowing andy characters to be added to the textField.
You could try something like below
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range
replacementString: (NSString*) string {
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString: string];
if([text length] > MAX_LENGTH)
return NO;
else
return YES;
}
EDIT1:
To this delegate method get called, set the textfield's delegate.
In this case, you can use it as
yourTextField.delegate = self;
In your viewWillAppear/viewDidAppear.
Try this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Prevent crashing undo bug – see note below.
if(range.length + range.location > textField.text.length)
{
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if(textField == self.yourTextField)
{
return newLength <= CHARACTER_LIMIT;
}
else
{
return YES;
}
}
Try this, Back space will work even if the textfield pre populated with more than MAX_Length text
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range
replacementString: (NSString*) string {
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString: string];
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString: string];
if([text length] > MAX_LENGTH && string.length>0)
return NO;
else
return YES;
}

Restrict user to enter abuse words in text view

I am developing a application where user has to enter only holy words. I want user to be restricted not to enter the abuse or adult word.
I have a big list of adult or abuse words whenever user will enter that word it should delete it automatically.
Any help will be appreciated.
You are probably using UITextField so you should look after forbidden words after text has changed:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSRange spaceRange = [newString rangeOfString:#" "];
if (spaceRange.location != NSNotFound) { // it's a new word
newString = [self stringWithoutForbiddenWords:newString];
}
textField.text = newString;
return NO; // we set the textField text manually
}
- (NSString *)stringWithoutForbiddenWords:(NSString *)string {
for (NSString *forbiddenWord in self.forbiddenWords) {
NSRange forbiddenWordRange = [string rangeOfString:forbiddenWord];
if (forbiddenWordRange.location != NSNotFound) {
// remove the forbidden word
string = [string stringByReplacingOccurrencesOfString:forbiddenWord withString:#""];
}
}
return string;
}
Don't forget to set you UITextField delegate.
Its a very simple logic , by the way only "Holy Word" seems very funny I hope you meant non-abusive words.
So to restrict abusive words, first make an Array and store all the abusive words in that.
then in textView shouldChangeTextInRange: check whenever user press " space.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#" "])
{
//now iterate the whole string and find whether any word contains any value from your Abusive words Array and replace the word with blank space or *
}

Unable to use backspace key to delete a character from a textfield in iOS

I am implementing the following delegate method for UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *integerPart = [textField.text componentsSeparatedByString:#"."][0];
NSString *decimalPart = [textField.text componentsSeparatedByString:#"."][1];
if ([integerPart length] > 8 || [decimalPart length] > 5) {
return NO;//this clause is always called.
}
...
}
I am trying to limit the number of digits entered in the textField to 6. The problem I have is that if I enter a number with 6 digits after the decimal, and then try to press the backspace key on my device to delete the numbers, or need to make a correction inside the number, I'm unable to.
The reason is that whenever it comes to this point in my code, it notices that I have already entered 6 digits after the decimal (which is correct), and thus, nullifies my backspace key entry. How do I maintain this limit of 6 digits after the decimal place, AND allow for editing of the number after reaching this limit?
I haven't had a chance to test this, but according to this answer, string should be empty when a backspace is entered (which makes sense). So you should be able to do this.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Always allow a backspace
if ([string isEqualToString:#""]) {
return YES;
}
// Otherwise check lengths
NSString *integerPart = [textField.text componentsSeparatedByString:#"."][0];
NSString *decimalPart = [textField.text componentsSeparatedByString:#"."][1];
if ([integerPart length] > 8 || [decimalPart length] > 5) {
return NO;//this clause is always called.
}
return YES;
}
//Construct the new string with new input
NSString* newText = [textField.text stringByReplacingCharactersInRange:range
withString:text];
NSString *integerPart = [newText componentsSeparatedByString:#"."][0];
NSString *decimalPart = [newText componentsSeparatedByString:#"."][1];
if ([integerPart length] > 8 || [decimalPart length] > 5) {
return NO;//this clause is always called.
}
I believe this is what you need. This will construct the new string that will be displayed in the textfield and you can evaluate that string.

Objective-c How to do validation on textField

I would like to prevent the user from not entering any data as well as entering only spaces. So basically there must at least be one character without a space. Then I would also like to remove any spaces at the beginning of the word so the first letter is a character that is not a space.
edit
the user must enter something and if the user enters a few spaces before it then I want to trim those spaces. I also want to prevent the user from just entering spaces.
Example
if the user enter's a name and surname like " James Dean" I would like to take the first space away but not the second space between James and Dean.
Set your UIViewController to be your destination UITextField's delegate and implement this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// verify the text field you wanna validate
if (textField == _nameTextField) {
// do not allow the first character to be space | do not allow more than one space
if ([string isEqualToString:#" "]) {
if (!textField.text.length)
return NO;
if ([[textField.text stringByReplacingCharactersInRange:range withString:string] rangeOfString:#" "].length)
return NO;
}
// allow backspace
if ([textField.text stringByReplacingCharactersInRange:range withString:string].length < textField.text.length) {
return YES;
}
// in case you need to limit the max number of characters
if ([textField.text stringByReplacingCharactersInRange:range withString:string].length > 30) {
return NO;
}
// limit the input to only the stuff in this character set, so no emoji or cirylic or any other insane characters
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "];
if ([string rangeOfCharacterFromSet:set].location == NSNotFound) {
return NO;
}
}
return YES;
}
try like this may be it helps to you,here is my code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string{
if([text.text length]==0){
if([string isEqualToString:#" "]){
return NO;
}
}
return YES;
}
By placing this code user won't enter space as a first letter but it accepts the space in the middle of the string.
I'll give you a hint for the first part.
NSString *tempname1 = [self.textField.text stringByReplacingOccurrencesOfString:#" " withString:#""];
BOOL thereAreJustSpaces = [tempname1 isEqualToString:#""];
-(void)removeSpacesFromTextFields:(id) sender {
NSString *trim = [self.FNTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([trim length] == 0) {
self.FNTextField.text = #"";
}
}
Try this, If you want to prevent space in your text field.
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(checkSpace:) name:UITextFieldTextDidChangeNotification object:textfield];
}
-(void)checkSpace:(NSNotification *)notification
{
str = [textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ;
textfield.text=str;
}

Using the same text field for URL and Google Search in iOS

I am trying to get the URL and google search in the same Text field.. the method I am using works alright but probably there is a better way. What I am doing is to check if there is a dot in the input like www.google.com, if a dot is not found, then search it on google..
NSRange range = [textField.text rangeOfString:#"."];
textField.text = (range.location != NSNotFound) ?
[NSString stringWithFormat:#"%#", textField.text] :
[#"http://www.google.com/search?q=" stringByAppendingString:textField.text ];
If the input has a dot, then search fails.. Is there a better way to do it? Thanks..
U can prevent user from entering . dot in UITextField like this by using UITextField's delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#"."])
{
return NO;
}
else
{
return YES;
}
}

Resources