CreditCardValidationScreen.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using CoreGraphics;
  3. using UIKit;
  4. namespace CreditCardValidation.iOS
  5. {
  6. public class CreditCardValidationScreen : UIViewController
  7. {
  8. UITextView creditCardTextField;
  9. UILabel errorMessagesTextField;
  10. UIButton validateButton;
  11. public override void ViewDidLoad()
  12. {
  13. base.ViewDidLoad();
  14. Title = "Credit Card Validation";
  15. View.BackgroundColor = UIColor.White;
  16. var frame = new CGRect(10, 130, 300, 30);
  17. creditCardTextField = new UITextView(frame);
  18. creditCardTextField.AccessibilityIdentifier = ("CreditCardTextField");
  19. creditCardTextField.Layer.BorderColor = UIColor.Black.CGColor;
  20. creditCardTextField.Layer.BorderWidth = 0.5f;
  21. creditCardTextField.Layer.CornerRadius = 5f;
  22. creditCardTextField.Font = UIFont.SystemFontOfSize(16);
  23. creditCardTextField.TextContainer.MaximumNumberOfLines = 1;
  24. creditCardTextField.KeyboardType = UIKeyboardType.NumberPad;
  25. validateButton = new UIButton(new CGRect(10, 165, 300, 40));
  26. validateButton.SetTitle("Validate Credit Card", UIControlState.Normal);
  27. validateButton.AccessibilityIdentifier = ("ValidateButton");
  28. validateButton.SetTitleColor(UIColor.White, UIControlState.Normal);
  29. validateButton.BackgroundColor = UIColor.FromRGB(52, 152, 219);
  30. validateButton.Layer.CornerRadius = 5;
  31. errorMessagesTextField = new UILabel(new CGRect(10, 210, 300, 40));
  32. errorMessagesTextField.AccessibilityIdentifier = ("ErrorMessagesTextField");
  33. errorMessagesTextField.Text = String.Empty;
  34. validateButton.TouchUpInside += (object sender, EventArgs e) =>{
  35. errorMessagesTextField.Text = String.Empty;
  36. // perform a simple "required" validation
  37. string errMessage;
  38. if (!IsCCValid(out errMessage))
  39. {
  40. // need to update on the main thread to change the border color
  41. InvokeOnMainThread(() =>{
  42. creditCardTextField.BackgroundColor = UIColor.Yellow;
  43. creditCardTextField.Layer.BorderColor = UIColor.Red.CGColor;
  44. creditCardTextField.Layer.BorderWidth = 3;
  45. creditCardTextField.Layer.CornerRadius = 5;
  46. errorMessagesTextField.Text = errMessage;
  47. });
  48. }
  49. else
  50. {
  51. NavigationController.PushViewController(new CreditCardValidationSuccess(), true);
  52. }
  53. };
  54. View.Add(creditCardTextField);
  55. View.Add(validateButton);
  56. View.Add(errorMessagesTextField);
  57. }
  58. bool IsCCValid(out string errMessage)
  59. {
  60. errMessage = "";
  61. if ((creditCardTextField.Text.Length < 16) || (string.IsNullOrWhiteSpace(creditCardTextField.Text)))
  62. {
  63. errMessage = "Credit card number is to short.";
  64. return false;
  65. }
  66. if (creditCardTextField.Text.Length > 16)
  67. {
  68. errMessage = "Credit card number is to long.";
  69. return false;
  70. }
  71. return true;
  72. }
  73. }
  74. }