-3

How do I check if a UITextField's text is a double or not?

Simple validation required, if the entered textfield value is a valid double value or not?

Valid double values: 1.00, 1.01 or 1.00001

Invalid double values: .0.1, .001.11, 1.0.1 or 1...0 etc.

mytxtfield.text=@"12.00"; //ok
mytxtfield.text=@"1.2..0"; // is invalid and so on

EDIT: My answer that works thanx sahzad ali

//------------
-(BOOL)isvalidDouble:(NSString*)txtstring
{

    NSArray *dotSeparratedArray = [txtstring componentsSeparatedByString:@"."];

    NSInteger count=[dotSeparratedArray count];
    count=count-1;

    if(count >1)
    {
        return TRUE ; //invalid
    }
    return FALSE; //valid


}
9to5ios
  • 5,319
  • 2
  • 37
  • 65
  • 1
    you can look into [string doubleValue] in your should change characters in range method. – Teja Nandamuri Feb 10 '16 at 19:28
  • i have to add validation for valid double value enter means, 1..1 or 1.. or 12.1.2 etc invalid only valid digita is if it contain only 1 decimal digit – 9to5ios Feb 10 '16 at 19:32
  • It would help if the question was more clear, update te question, don't rely on the comments for more information. Ex: 1, 12., 1., 12.1, 1.2, 123.45, 123.4, 12.0 – zaph Feb 10 '16 at 19:35
  • @Lou Franco I don't think that is what thw OP means, thise are compiler syntax errors. – zaph Feb 10 '16 at 19:38
  • just need simple validation if enter textfield value is double or not or invalid double value Valid double value: 1.0 or 1.00 or 1.01 Invalid double value 1..0 or 0.1.0 etc – 9to5ios Feb 10 '16 at 19:38
  • 1
    See http://stackoverflow.com/questions/12865981/parsing-nsstring-to-double – TotoroTotoro Feb 10 '16 at 19:40
  • Must it be a double, what about an integer. In any event, if you are not unless you improve the question good answers can't be provided. – zaph Feb 10 '16 at 19:40
  • What about "0", "00", "00.", "00.000", "1.0e12", "-3", "+3", " 1 ", " 1 1", "1.1 and so on", and so on? – gnasher729 Feb 10 '16 at 21:04
  • What about all of the people living in locales that don't format decimal numbers with a period? The number `1234,56` is perfectly valid for lots of people. Or `1.234,56` or `1 234,56` or `1.234.567,89`. Or many other possible formats. Use `NSNumberFormatter`. Do NOT assume a valid number has a period (and just one). – rmaddy Feb 10 '16 at 22:13

2 Answers2

1

You can limit user from entering multiple dots or you can use same logic in your TextFieldDidEndEditing method. If componentsSeparatedByString returns greater than 2 it means there are multiple dots.

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *myString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSArray *dotSeparratedArray = [myString componentsSeparatedByString:@"."];
    if([dotSeparratedArray count] >= 2)
    { 
        NSString *string1=[NSString stringWithFormat:@"%@",[dotSeparratedArray objectAtIndex:1]];
        return !([string1 length]>1);
    }
    return YES;
}
Shehzad Ali
  • 1,846
  • 10
  • 16
  • This won't work in locales that use the comma (`,`') as the decimal separator. – rmaddy Feb 10 '16 at 22:10
  • Problem was user must not be able to enter multiple dots. Once that problem has been resolved. He may have formatter for that or he can write formatter for that. – Shehzad Ali Feb 11 '16 at 07:13
  • The problem is to validate that the string entered by a user represents a valid decimal number. It's far more than just checking for two or more dots. The OP didn't understand the full nature of the problem. So the examples given by the OP don't represent reality. – rmaddy Feb 11 '16 at 13:46
1

I don't know whether or not this will serve your purposes, but you could use NSNumberFormatter and customize it to your needs:

NSNumberFormatter *myFormatter = [[NSNumberFormatter alloc] init];

[myFormatter setFormatWidth:7];
[myFormatter setPaddingCharacter:@"0"];
[myFormatter setMinimumSignificantDigits:0];

[myFormatter setMinimum:@0];
[myFormatter setMaximum:@9999999];
[myClientIDTextField setFormatter:myFormatter];
SevenBits
  • 2,836
  • 1
  • 20
  • 33