Forum Xamarin.Forms

Which is the proper way of validations of forms?

I have registration form. There are fields like Email, password etc. How to implement validations?
On submit of form, required validations & textbox should be highlighted.

Answers

  • @RichaGajjar
    string username_Input, password_Input;
    public Open()
    {
    InitializeComponent();
    this.Title = "Login page";
    }

        void Handle_Clickeds(object sender, System.EventArgs e)
        {
            username_Input = username.Text;
            password_Input = password.Text;
            if (username_Input == null)
            {
                DisplayAlert("Alert", "please enter userName", "ok");
            }
            else if (password_Input == null)
            {
                DisplayAlert("Alert", "please enter password", "ok");
            }
    
            else
            {
                loading.IsVisible = true;
                LoginAsync();
            }
        }
    

    }

  • RichaGajjarRichaGajjar USMember ✭✭

    May be its not a proper way.
    I have registration form contains around 15 fields, so for each n every fields i could not display alert!

  • @RichaGajjar
    The best way to do validation is Behavior.

    Will demonstrate here using behavior implementation : -

    step 1 : First create a class "EmailValidator"

    _public class EmailValidator : Behavior
    {

        const string emailRegex = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
    
            static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(EmailValidatorBehavior), false);
    
        public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
    
        public bool IsValid
        {
            get { return (bool)base.GetValue(IsValidProperty); }
            private set { base.SetValue(IsValidPropertyKey, value); }
        }
    
        protected override void OnAttachedTo(Entry bindable)
        {
            bindable.TextChanged += HandleTextChanged;
        }
    
        void HandleTextChanged(object sender, TextChangedEventArgs e)
        {
            IsValid = (Regex.IsMatch(e.NewTextValue, emailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
            ((Entry)sender).TextColor = IsValid ? Color.Default : Color.Red;
        }
    
        protected override void OnDetachingFrom(Entry bindable)
        {
            bindable.TextChanged -= HandleTextChanged;
    
        }
    }_
    

    Step 2: Assign it to your controls.

    entryEmail=new Entry
    {
    Text ="",
    Keyboard = Keyboard.Email,
    Behaviors = {new EmailValidatorBehavior{}},
    Placeholder=formattedString.ToString(),
    };

    After that you will see if you are writing wrong email in entry the text should be red.

  • rajeshwaranrajeshwaran Member ✭✭

    void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
    var email = Emailentry.Text;

            var emailPattern = @"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$";
            if (Regex.IsMatch(email, emailPattern))
            {
    
                ErrorLabel.Text = "Email is valid";
            }
            else
            {
    
                ErrorLabel.Text = "EMail is InValid";
            }
        }
    

    this code works 100% in simple way

  • rajeshwaranrajeshwaran Member ✭✭

  • abdulisticabdulistic Member

    How can we user Fluent Validation in Xamarin Android?

  • mohamed_dawoodmohamed_dawood Member ✭✭

    You can find a good solution here
    https: //github.com/m0dawood/PQXamarin

  • MarkZhukovskyMarkZhukovsky USMember ✭✭

    I wrote a no-frills way to help make it a lot easier (imho) to use FluentValidation in Xamarin: https://github.com/mzhukovs/FluentValidationRulesPlugin

    Included in the repo is a full example.

Sign In or Register to comment.