1

My school is running a project to build a website that containts register and login . Of course , the level of the instructors in my country is low , and we are required to use Javascript to check the inputs in the form , instead of using ASP.NET to do it .

So , let's get to the question :

I'm having few inputs , like this :

<asp:TextBox id="usname" runat="server" MaxLength="20" value="שם משתמש" TextMode="SingleLine" CssClass="usernameInput" onfocus="this.value=''"></asp:TextBox>
            <asp:TextBox ID="email" runat="server" MaxLength="40" value="אימייל" TextMode="SingleLine" CssClass="emailInput" onfocus="this.value=''"></asp:TextBox>
            <asp:TextBox ID="password" runat="server" MaxLength="20" value="סיסמה" TextMode="Password" CssClass="passwordInput" onfocus="this.value=''"></asp:TextBox>
            <asp:TextBox ID="passwordRepeat" runat="server" MaxLength="20" value="אימות סיסמה" TextMode="Password" CssClass="passwordRepeatPassword" onfocus="this.value=''"></asp:TextBox>

While trying to identify the field with it's id , Javascript won't recognize it . What can I do in order to keep this Textboxes , and check the input with Javascript ?

Thanks in advance , Iliya

1

2 Answers 2

4

ASP.Net generates its own unique IDs for server-side controls.

You can prevent this by setting the ClientIDMode property to Static.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try with the following option:

document.getElementById('<%=ControlId.ClientID %>');

Example:

function CheckInput()
{
    var userName = document.getElementById('<%=usname.ClientID %>');
    var mail = document.getElementById('<%=email.ClientID %>');
    var pwd = document.getElementById('<%=password.ClientID %>');
    var pwdRepeat = document.getElementById('<%=passwordRepeat.ClientID %>');

    // Your validation logic

    if (userName == "XYZ" && mail == "[email protected]" && pwd == "1234" &&
        pwdRepeat == "1234")
    {
        return true;
    }

    else
    {
        return false;
    }
}

And you can call this from your button click as follows:

<asp:Button ID="Button1" runat="server" Text="Button" 
 OnClientClick="javascript:return CheckInput();" />

The above javascript will either return true or false. If it returns True then the button will postback to server.

1 Comment

Thanks to all of you ! How can I make the site use the Javascript function when pressing on the submit , and if it returned true , to do the submit to the Code Behind ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.