iPhone: UITextfield validation - iphone

I checked the existing examples here in S.O, for my case which one do you think is better,
There are many textfields in one view and I want to validate them just when the user is editing that particular textfield, cause otherwise I have to validate them together when user press next on the screen than I have show all validation messages which I dont want to bother with. I want to validate the textfield in 2 different ways, if its a number input I want to validate it in a defined range e.g if number between between 5 and 1000 or if its a text then if the lenght of the text is in a defined range e.g between 2 and 10 characters. And the user must be able to enter any input out of range.
Which is better;
Using textField:shouldChangeCharactersInRange
or something like this:
[textField addTarget:self action:#selector(validateField:) forControlEvents:UIControlEventEditingChanged];
And either the case how can I check on runtime that the number entered is in range and dont allow user to enter bigger or smaller numbers

Good one.
The answer for your question is
1) Set the tag values to all the textfields.
2) Use UIControlEventEditingDidBegin and trigger a method to validate the textfield before the currentone.
[textfield1 setTag:1];
[textfield2 setTag:2];// make sure to set tag values incrementally
[textfield2 addTarget:self #selector(validate:) forControlEvents:UIControlEventEditingDidBegin];
-(void) validate:(id) sender
{
// 10 is the number of textfields, change it accordingly.
if(sender.tag!=1 && sender.tag!=10)
{
//validate textfield with tag sender.tag-1
}
else
{
//handle first and last textfield here
}
}
Try this, probably this will solve your problem.

Actually, both of your approaches will be just fine! Using the delegate of the UITextField to call textField:shouldChangeCharactersInRange and perform the validation there is an equally good approach as using Target & Action. In both situations, you define a handler that receives the calling UITextField as an argument (one implementation of this is what #Anil Kumar suggested in his answer to tag the text fields. but this is actually not even required, you can just call isEqual: on the text fields directly to check for equality).
Personally, I tend to use delegation for these kinds of tasks though.
Update: If you want to go really fancy on this problem, you'll use Reactive Cocoa. The April Tech Talk of the Ray Wenderlich Gang had a great session on it and also discusses the issue of from validation, so it'll just match your case! :)

Take a look at TextFieldValidator developed in swift

Related

Best approach getting answer from multiplay text boxes

I've created multiple text fields where each text field accepts only 1 character and then moves to the next box. I would like to check users answer when all the text fields are filled but I'm a bit confused as to how to do so.
My first approach was to create an answer String and every time textFieldDidChange is called I add to the String the textfield.text but how can I know when all the boxes been filled and call the checkAnswer function another issue is that if the user decides to fill the boxes in a different order then when I compare the user answer to the correct answer it obviously comes incorrect as the order is different.
[here's a pic of the boxes every game round it generates a different number of boxes depending on the answer
If you know how many number of textFields you have then, you can add a property observer to yourAnswerString and as soon as the value of yourAnswerString becomes equal to numberOfTextFields, it will call the check Answer function automatically.
var yourAnswerString = "" {
didSet {
if yourAnswerString.count == numberOfTextFields {
self.checkAnswer()
}
}
}

GXT 3 spinnerField validation

I want to validate that user cannot change spinner value manually by typing in text box of spinner.
For example a field sales multiple = x which I fetched from server not fix.
and displays a spinner field with limitation of like bellow
spinner.setMinValue = x
spinner.setIncrement = x
spinner.setValue = x
so user forcefully select a value which is multiple with x. e.g. if x=3 the user have to enter 3,6,9... and so on.
So here my issue is if I type a 2 in spinner field text box. GXT widget accept that value.
Posible solutions:
Is there any predefined properties of spinnerfield that i forget to set it?
Is there any predefined validator for this?
Can I set text box of spinner field read only by css so user cannot focus on text box but still change a value.
If none of above how to achieve manually?
i've searched a bit in the different classes and I don't see either a precise method which would set what you want.
Don't know about one, and even with one, a validator doesn't change the value in the input field, but maybe it's enough for your needs.
You can disable the text input by calling setEditable(boolean) on the spinnerfield (testSpinner.setEditable(false);)
Maybe you could search around the IntegerPropertyEditor, I haven't tried but as long as a new Spinner is like this:
SpinnerField<Integer> testSpinner = new SpinnerField<Integer>(new NumberPropertyEditor.IntegerPropertyEditor());
you can seen that there is another Constructor for IntegerPropertyEditor, which takes a "NumberFormat" param, but there is no NumberFormart() constructor, so I'm not sure about how you create your own one, but that could be an idea (to format numbers in the input to be a multiple of the increment).
The last option would be that Sencha forgot this possibility and that you should report this as a "bug" on the forum ?
Hope to have helped a bit, good luck :).

Identify multiple UITextField and the row index

Here is my problem:
I have an array of model class(Let's say, 'addressModel' with fields address, street and city.). Now i have a custom cell with three UITextFields for the three fields in the model class. Once the user ends editin, i want to (validate &) add the data in the particular textfield to the respective model object. (Ex: user ended editing addressTextField, then addressModel.address = addressTextField.text).
How can we identify the textfield that the user selected and the indexPath.row? In my case i need to know both? Any help?
Thank you.
If your view controller implements the UITextFieldDelegate protocol, you can receive the textFieldDidEndEditing method call. In there, you can get the tag of the field that the user was just editing. When creating your cells you can specify some kind of integer tag scheme so that you can reverse engineer what section, row, and specific field the user was actually editing at the time. You could do something like:
textField.tag = (indexPath.section * 10000) + (indexPath.row * 100) + (textFieldIndex);
Assuming you don't have more than 100 textFields per row, or 100 rows per section, this particular scheme should work.
You can validate data when user finishes editing I mean you can validate data in UITextField delegate method:
- (void)textFieldDidEndEditing:(UITextField *)textField;
here no need to find out which textfield is edited since you will get that textfield.And if you want to identified which row then you can assign tag which is nothing but row, to textfield and on that basis you will come to know which row textfield is edited.
set the separate tag of each text field .. and on didEndEditing: method use the following code....
if(textField.teg == FIRST_TAG){
// do your code
}
else if(textField.teg == SECOND_TAG){
// do your code
}...
and soo on....
may this will help you...

Can I not remove the return key on the keyboard? Or at least change its text to "Set"?

I am creating a sign up page for my iphone app and am having some problems making the way the input views work for the different kinds of fields consistent. The fields are listed as cells in a table view and the editing is supposed to take place directly in the table by having appropriate input views sliding up from the bottom.
Let me focus on only two of the fields here, namely the username field and the birthday field: for the username field it makes sense to have an ASCII capable keyboard sliding up when the user presses the field whereas a date picker seems more useful in the birthday field case.
For both the keyboard and the date picker the cancel button could be located in a tool bar just above the input view. But what about the set button? If I put that in the toolbar as well I need the return key in the keyboard to go away! But that is not possible is it?
If the return key cannot be removed then I might have to live with the set button in the toolbar only in the birthday case and then use the return key as the set button in the username case - but can I then at least change the text on the return key to "Set"?
No it's not possible* to hide the "Return" key.
It is not possible to set the string to "Set" either, but could use the .returnKeyType property to change it to a limited set of strings.
theTextField.returnKeyType = UIReturnKeyDone;
(* Well you could put an opaque UIView directly above it but it's a very bad practice and generally breaks if the user chooses a different input method)

how can I show only number format input in UITextField?

I am making simple application that will take hotel name and price of item, and I want to show the price in only number format, because it can not be in alphabatical format, so what should I do add in code so that only number will be inputted in the Price UITextField?
If you are not getting my question, you may ask anything again
I do appreciate if I will get proper way of doing this,
If you want your text field to accept only the numbers, you can set it's keyboardType property to UIKeyboardTypeNumberPad.
[yourTextField setKeyboardType:UIKeyboardTypeNumberPad];
There are different types of keyboardTypes available, which you can refer in UITextInputTraits Protocol Reference.
This could be done in the Interface Builder as well: