-2

How to validate Textfields with greater than 0 value. i tried all suggestions of stack overflow i failed to validate it. and how to check value of textfield less than or equal to my label value.

 let inputStr = totalAmountTextfield.text as? String 
    let inputInt = Int(inputStr!)

    if (inputInt)! < 0 
   {
     let alert = UIAlertView() alert.title = "Message" 
    alert.message = "Enter Valid Amount" 
    alert.addButton(withTitle: "Ok") 
    alert.delegate = self alert.show() }
    else{ print("pay pressed")
     razorpay.open(options) } 
    } 
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ram
  • 858
  • 1
  • 14
  • 19
  • Possible duplicate of [What are best practices for validating email addresses in Objective-C for iOS 2.0?](https://stackoverflow.com/questions/800123/what-are-best-practices-for-validating-email-addresses-in-objective-c-for-ios-2) – Prashant Tukadiya Oct 30 '17 at 06:10
  • Show what you have already tried. – PGDev Oct 30 '17 at 06:12
  • check this.. https://stackoverflow.com/questions/41023145/validate-text-fields-swift-3 – Nirav Kotecha Oct 30 '17 at 06:12
  • let inputStr = totalAmountTextfield.text as? String let inputInt = Int(inputStr!) if (inputInt)! < 0 { let alert = UIAlertView() alert.title = "Message" alert.message = "Enter Valid Amount" alert.addButton(withTitle: "Ok") alert.delegate = self alert.show() }else{ print("pay pressed") razorpay.open(options) } } – Ram Oct 30 '17 at 06:14
  • The above code i tried PGDev – Ram Oct 30 '17 at 06:15

1 Answers1

0

Simple convert your input string to double and check with that

enter image description here

   class ViewController: UIViewController {


        @IBOutlet weak var txt: UITextField!

        @IBAction func onClickSubmit(_ sender: Any)
        {
            let inputStr:String = txt.text ?? ""

            let alert = UIAlertController()
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))

            if inputStr.isEmpty
            {
                alert.message = "Please Enter Text"
                self.present(alert, animated: true, completion: nil)

            }
            else
            {
                let inputInt = inputStr.integerValue

                if (inputInt)! < 0
                {
                    alert.message = "Enter Valid amount"
                       self.present(alert, animated: true, completion: nil)

                }

                else
                {
                    print("Pay to Proceed")
                }
            }

        }

    }

    extension String
    {

        func isNumber() -> Bool{
            let numberCharacters = CharacterSet.decimalDigits.inverted
            return !self.isEmpty && self.rangeOfCharacter(from:numberCharacters) == nil
        }
        struct NumberFormat
        {
            static let instance = NumberFormatter()
        }
        var integerValue:Int?
        {
            if self.isEmpty
            {
                return 0
            }
            else
            {
                return (NumberFormat.instance.number(from: self)?.intValue) ?? 0
            }
        }
    }
Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • at same the time how to check textfield empty or not – Ram Oct 30 '17 at 06:24
  • you can check it before converting, but using above `extension` even if you pass blank string it will return 0. so you show alert – Jaydeep Vyas Oct 30 '17 at 06:35
  • let inputStr:String = "" in this line of code you are comparing with string variable but in my case i am using textfield, how can i use your code. – Ram Oct 30 '17 at 08:47
  • i am new to ios thats why little confusion about it. – Ram Oct 31 '17 at 06:26
  • let inputInt = (totalAmountTextfield.text as? integerValue) here i am getting error Use of undeclared type 'integerValue' in second condition can you help me how to fix this issues – Ram Oct 31 '17 at 06:27
  • write `(totalAmountTextfield.text as? String).integerValue` – Jaydeep Vyas Oct 31 '17 at 07:15
  • Value of type 'String?' has no member 'integerValu' i am getting this error – Ram Oct 31 '17 at 07:28
  • @kishan write the above code `let inputStr:String = (totalAmountTextfield.text as? String) ?? ""` then use inputStr why are you using totalAmount.text u can simply use inputStr – Jaydeep Vyas Oct 31 '17 at 07:29
  • i am saying about this let inputInt = inputStr.integerValue if (inputInt)! < 0 { – Ram Oct 31 '17 at 07:36
  • ya then what is problem above code is working perfectly – Jaydeep Vyas Oct 31 '17 at 08:02
  • can you give sample for message validation – Ram Oct 31 '17 at 08:38
  • yeah thanks great explination. please send sample code for string validation. – Ram Oct 31 '17 at 09:42