Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I learning .Net Technologies. I tried to validate (i.e Basic validation, whether data is entered in all fields or not) the form using javascript below is my code, when I run this code, an unknown exception
0x800a138f - Microsoft JScript runtime error: Object expected
is getting raised. Can anyone please provide the solution.

HTML
<head runat="server">
    <title></title>
    <script type="text/javascript">
       function validateForm()
        {
           var f=document.forms["form1"];


        if(f.txtUName.value==null||f.txtUName.value=="")
        {
        alert("Please provide your name");
        f.txtUName.focus();
        return false;
        }

        if(f.txtEmail.value==null||f.txtEmail.value="")
        {
        alert("Please provide your email-id");
        f.txtEmail.focus();
        return false;
        }

        if(f.txtPwd.value==null||f.txtPwd.value=="")
        {
        alert("Please enter password");
        f.txtPwd.focus();
        return false;
        }

        if(f.txtCPwd.value==null||f.txtCPwd.value="")
        {
        alert("Please confirm your password");
        f.txtCPwd.focus();
        return false;
        }

        return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" onclick="javascript: validateForm();" onsubmit="javascript: validateForm();">
    <div>
    <h2 style="text-align:center">Registration</h2>
    <table id="tblReg">
        <tr><td>User Name</td>
            <td>:</td>
            <td><asp:TextBox ID="txtUName" runat="server" />
                <asp:RequiredFieldValidator ID="rfvUName" runat="server" ControlToValidate="txtUName" ForeColor="Red" ErrorMessage="Please enter User name" />
            </td>
        </tr>
        <tr><td>Email</td>
            <td>:</td>
            <td><asp:TextBox ID="txtEmail" runat="server" />
                 <asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="txtEmail" ForeColor="Red" ErrorMessage="Please enter email" />
            </td>
        </tr>
        <tr>
            <td>Password</td>
            <td>:</td>
            <td><asp:TextBox ID="txtPwd" runat="server" TextMode="Password" />
                 <asp:RequiredFieldValidator ID="rfvPwd" runat="server" ControlToValidate="txtPwd" ForeColor="Red" ErrorMessage="Please enter password" />
            </td>
        </tr>
        <tr>
            <td>Confirm Password</td>
            <td>:</td>
            <td><asp:TextBox ID="txtCPwd" runat="server" TextMode="Password" />
                 <asp:RequiredFieldValidator ID="rfvCPwd" runat="server" ControlToValidate="txtUName" ForeColor="Red" ErrorMessage="This field should not be an empty field" />
            </td>
        </tr>
        <tr>
            <td></td>
            <td><asp:Button ID="btnReg" runat="server" OnClick="btnReg_Click" Text="Click"/></td>&nbsp;&nbsp;
            <td><asp:Button ID="btnCancel" runat="server" OnClientClick="return clearAll()" Text="Cancel"/></td>
        </tr>
    </table><br />
        <asp:Label ID="lblLink" runat="server" Text="Already registered click below to Login" /><br />
        <asp:HyperLink ID="hLnkLogin" runat="server" Text="Login" NavigateUrl="Log.aspx" />
    </div>
    </form>
Posted
Updated 2-Jun-15 3:07am
v2

Hi,

Please check and rectify few things mentioned below.

XML
Are you including the jQuery script file into this page? That might explain issues with error messages about the "$" variable.

Use click() instead of onclick() to fire click events. onclick is a handler that is fired when the element is clicked, but is not used to initiate the event itself.

The element's id might not be what you have defined in the page due to ASP.NET namespacing and its goals of ensuring unique element ids on the client. An easy way to figure this all out is to View Source on a rendered page and find your button and its "real" id.

As mentioned in the comments to this post, the jQuery script must referenced by the page via a <script> tag for the jQuery functions to work.



Also don't forget to clear your browser's cache.

Thanks
 
Share this answer
 
Comments
F-ES Sitecore 2-Jun-15 9:36am    
He's not using jQuery.
When you get an error always say what line the error is on.

I wouldn't bother spending any time on this TBH, as you are using the .net validator controls just add EnableClientScript="true" attribute to your asp validator controls and .net will generate the js for you.
 
Share this answer
 
Comments
bhvch 3-Jun-15 0:26am    
error is coming at <form....> tag.
F-ES Sitecore 3-Jun-15 4:24am    
Don't bother with your own javascript, use the client script generated by the validation controls you're already using.

<asp:RequiredFieldValidator ID="rfvCPwd" runat="server" EnableClientScript="true" ControlToValidate="txtUName" ForeColor="Red" ErrorMessage="This field should not be an empty field" />

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900