title: how to validate email with jquery
body: <p>I have an input form which asks the user to enter its username and password. once the user enters the data, and clicks the submit button , the jquery will run a post request and send the information to the controller /Home/RegisterUser, but all im getting is </p>
  
  <p>An exception of type 'System.NullReferenceException' occurred in email2.dll but was not handled in user code</p>
  
  <p>Additional information: Object reference not set to an instance of an object.</p>
  
  <p>for some reason the ajax request is not able to obtain the values email and password, as they are set to null ie  email = null.</p>
  
  <p>Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446</p>
  
  <p>WebForm1.aspx</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="email2.WebForm1" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;script src="Scripts/jquery-1.10.2.js" type="text/javascript" &gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"  &gt;
      &lt;div&gt;
          &lt;asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"&gt;
          &lt;/asp:ScriptManager&gt;
          &lt;fieldset style="width: 200px;"&gt;
              &lt;asp:Label ID="lblEmailAddress" runat="server" Text="Email Address"&gt;&lt;/asp:Label&gt;
              &lt;asp:TextBox ID="txtEmail" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;asp:Label ID="lblPassword" runat="server" Text="Password"&gt;&lt;/asp:Label&gt;
              &lt;asp:TextBox ID="txtPassword" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;/fieldset&gt;
          &lt;div&gt;
          &lt;/div&gt;
          &lt;asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="Signup();return false;" /&gt;
      &lt;/div&gt;
  
          &lt;div id="postResult"&gt;?&lt;/div&gt;
  
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  // use  PageMethods. if want to use methods within WebForm1.apsx.cs
  
  &lt;%--&lt;script type="text/javascript"&gt;
      function Signup() {
          var email = document.getElementById('&lt;%=txtEmail.ClientID %&gt;').value;
          var password = document.getElementById('&lt;%=txtPassword.ClientID %&gt;').value;
  
          PageMethods.RegisterUser(email, password, onSucess, onError);
  
          function onSucess(result) {
              alert(result);
          }
  
          function onError(result) {
              alert('Cannot process your request at the moment, please try later.');
          }
      }
  &lt;/script&gt;--%&gt;
  
  &lt;%--&lt;script type="text/javascript"&gt;
  
  
      var url = "/Home/RegisterUser";
  
      var email = document.getElementById('&lt;%=txtEmail.ClientID %&gt;').value;
      var password = document.getElementById('&lt;%=txtPassword.ClientID %&gt;').value;
  
      var f = $("form1");
      var url = f.attr("action");
      var formData = f.serialize();
      $.post(url, formData, function (data) {
          $("#postResult").html(data);
      });
  
  
  &lt;/script&gt;
  --%&gt;
  
  &lt;script type="text/javascript"&gt;
  
  
      var username = $("input#txtEmail").val();
      var password = $("input#txtPassword").val();
  
  
      $(document).ready(function () {
  
          $("#btnCreateAccount").click(function () {
              debugger;
  
              $.ajax({
                  type: 'POST',
                  url: 'Home/RegisterUser',
                  data: { username: username, password: password },
                  ContentType: 'application/Json',
                  dataType: "json",
                  success: function (data) {
                      alert("Register succesfully");
                      $("#postResult").html(data);
                  },
                  error: function () {
                      alert("Error while registration");
  
                  }
              });
          });
      });
  
  
  
  
      &lt;/script&gt;
  </code></pre>
  
  <p>HomeController.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Script.Services;
  //need this to run  [WebMethod]
  using System.Web.Services;
  
  namespace email2.Controllers
  {
      public class HomeController : Controller
      {
          public ActionResult Index()
          {
              return View();
          }
  
          public ActionResult About()
          {
              ViewBag.Message = "Your application description page.";
  
              return View();
          }
  
          public ActionResult Contact()
          {
              ViewBag.Message = "Your contact page.";
  
              return View();
          }
  
  
          [WebMethod]
          [AcceptVerbs(HttpVerbs.Post)]
          [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
          public ActionResult RegisterUser(string email, string password)
          {
              string result = "Congratulations!!! your account has been created.";
              if (email.Length == 0)//Zero length check
              {
                  result = "Email Address cannot be blank";
              }
              else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
              {
                  result = "Not a valid email address";
              }
              else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
              {
                  result = "Not a valid email address";
              }
  
              else if (password.Length == 0)
              {
                  result = "Password cannot be blank";
              }
              else if (password.Length &lt; 5)
              {
                  result = "Password canonot be less than 5 chars";
              }
  
              return Content(result);
          }
  
  
  
      }
  }
  </code></pre>