asp.net - how to validate email with jquery -
i have input form asks user enter username , password. once user enters data, , clicks submit button , jquery run post request , send information controller /home/registeruser, im getting
an exception of type 'system.nullreferenceexception' occurred in email2.dll not handled in user code
additional information: object reference not set instance of object.
for reason ajax request not able obtain values email , password, set null ie email = null.
version information: microsoft .net framework version:4.0.30319; asp.net version:4.0.30319.18446
webform1.aspx
<%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="email2.webform1" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="scripts/jquery-1.10.2.js" type="text/javascript" ></script> </head> <body> <form id="form1" runat="server" > <div> <asp:scriptmanager id="scriptmanager1" runat="server" enablepagemethods="true"> </asp:scriptmanager> <fieldset style="width: 200px;"> <asp:label id="lblemailaddress" runat="server" text="email address"></asp:label> <asp:textbox id="txtemail" runat="server"></asp:textbox> <asp:label id="lblpassword" runat="server" text="password"></asp:label> <asp:textbox id="txtpassword" runat="server"></asp:textbox> </fieldset> <div> </div> <asp:button id="btncreateaccount" runat="server" text="signup" onclientclick="signup();return false;" /> </div> <div id="postresult">?</div> </form> </body> </html> // use pagemethods. if want use methods within webform1.apsx.cs <%--<script type="text/javascript"> function signup() { var email = document.getelementbyid('<%=txtemail.clientid %>').value; var password = document.getelementbyid('<%=txtpassword.clientid %>').value; pagemethods.registeruser(email, password, onsucess, onerror); function onsucess(result) { alert(result); } function onerror(result) { alert('cannot process request @ moment, please try later.'); } } </script>--%> <%--<script type="text/javascript"> var url = "/home/registeruser"; var email = document.getelementbyid('<%=txtemail.clientid %>').value; var password = document.getelementbyid('<%=txtpassword.clientid %>').value; var f = $("form1"); var url = f.attr("action"); var formdata = f.serialize(); $.post(url, formdata, function (data) { $("#postresult").html(data); }); </script> --%> <script type="text/javascript"> 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"); } }); }); }); </script>
homecontroller.cs
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.web.script.services; //need 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!!! account has been created."; if (email.length == 0)//zero length check { result = "email address cannot blank"; } else if (!email.contains(".") || !email.contains("@")) //some other basic checks { result = "not valid email address"; } else if (!email.contains(".") || !email.contains("@")) //some other basic checks { result = "not valid email address"; } else if (password.length == 0) { result = "password cannot blank"; } else if (password.length < 5) { result = "password canonot less 5 chars"; } return content(result); } } }
it should be
public actionresult registeruser(string email, string password) { string result = "yeah!"; //... checks return content(result); }
instead of
public static string registeruser(string email, string password) { //.... }
your action can't found if it's static
Comments
Post a Comment