Xamarin.Forms validate textfields in a webview to show iOS native popup

92 Views Asked by At

I have done some research online, but have found nothing that directly answers my question here. Is there a way to validate a WebView form via Xamarin.Forms?

In my exact scenario, I would like to assure that if there is a textfield that is left empty within the WebView then I will display a native iOS alert (DisplayAlert).

My asynchronous Task looks somewhat like below:

private async Task<string> FormValidation ()
    {
        WebView wv = new WebView();

       string elementID = "mobileShipFromCsp1";
       string x = await wv.EvaluateJavaScriptAsync($"document.getElementById('{elementID}').value;");
        Console.WriteLine("This is x: " + x);

        if(x == "")
        {
            await DisplayAlert("", "This should work", "Ok");
        }
        Console.WriteLine("This is x: " + x);
        return x;

    }

But there is no alert showing at the press of submit.

I cannot bind the submit button to this Task, seeing that there is no button in the xaml (because we are calling a webview).

This is my XAML:

<?xml version="1.0" encoding="UTF-8"?>
 <views:WebViewContainerBaseView xmlns="http://xamarin.com/schemas/2014/forms" 
 <ContentPage.Content>
    <StackLayout> 
   <webviews:WebviewContainer x:Name="shippingView" VerticalOptions="FillAndExpand"  

    BackgroundColor="white"/>
    </StackLayout> 

      </ContentPage.Content> 
      </views:WebViewContainerBaseView>
0

There are 0 best solutions below