Using an alertbox when the length is wrong

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • preetityagi
    New Member
    • Nov 2009
    • 2

    #1

    Using an alertbox when the length is wrong

    i put this code but there is a problem. this is not working properly.
    this is on textchanged evet .

    Code:
    protected void t1_TextChanged(object sender, EventArgs e)
        {
            int max = 5;
            int length = t1.Text.Length;
            if (length !=max)
            {
                Response.Write("<script type = text/javascript language = javascript >");
                t1.Attributes.Add("onblur", "alert('" + length + "');");
                //Page.Validate();
             // Response.Write("alert('" + length + "');");  
                //t1.Focus();
                
                Response.Write("</script>");
            }
    Last edited by Frinavale; Nov 10 '09, 03:47 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags and moved to ASP.NET from ASP Classic.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I'm not sure what you're trying to do.
    Right now you are handling the .NET "TextChange d" event.
    When the text in the TextBox (t1) is changed a postback to the server occurs.
    Then your .NET code is executed.

    You have a bunch of JavaScript stuff going on here that I can't understand.

    JavaScript is run in the web browser (client side).
    It is probably best that you use JavaScript to handle the TextChanged event in the browser instead of posting back to the server....so you have the right idea, I just don't know what you're doing.

    Another thing that is not a good idea is use the Response.Write( ) method in your C# code. The reason is because when you use this method in your C# code it is very likely that it will be placed somewhere invalid (like outside of the <html> tag).
    For example, if I had the following C# code:
    Code:
    Response.Write("<script type = 'text/javascript'>");
    Response.Write("  alert('Hello World!');");
    Response.Write("</script>");
    The resulting HTML would be something like:
    Code:
    <script type='text/javascript'>
      alert('Hello World!');
    </script>
    <!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">
    ......
    Notice that the script is before the <html> tag?
    This is makes the web page invalid and some browsers wont even run the code, other browsers will try to run the code but it may not run as expected.


    I recommend putting the JavaScript into the ASPX page itself, or placing it into an external JavaScript file.

    If you place the JavaScript in the ASPX page itself then you can use ASP to tell the Response.Write method where the content should be placed.

    For example, the following JavaScript code is located with my Default.aspx page:
    Code:
    <script type="text/javascript">
    function onTextBoxBlurEventHandler(){
      var textBoxElement = document.getElementById('<% Response.Write(t1.ClientID); %>');
      if(textBoxElement && textBoxElement.value){
        alert("The length is: "+textBoxElement.value.length());
      }
    }</script>
    Notice how I used the Response.Write method?
    I told it to write the ClientID of the t1 TextBox into the getElementById JavaScript method... Now the JavaScript is placed in the right place on the page and everything is valid.


    This script doesn't even really need to use the getElementById JavaScript method...and it doesn't require the use of the Response.Write method either.
    I would modify the JavaScript function so that it took 1 parameter: the TextBox element.
    Code:
    <script type="text/javascript">
    function onTextBoxBlurEventHandler(textBoxElement){
      if(textBoxElement && textBoxElement.value){
        alert("The length is: "+textBoxElement.value.length());
      }
    }</script>

    I would remove the "TextChange d" event from the t1 TextBox and in the page load event I would put:

    Code:
    t1.Attributes.Add("onblur","onTextBoxBlurEventHandler(this);');
    Happy coding,

    -Frinny

    Comment

    • manmadhan
      New Member
      • Sep 2009
      • 27

      #3
      alert("The length is: "+textBoxElemen t.value.length( ));
      use it within scripit tag

      Comment

      • PRR
        Recognized Expert Contributor
        • Dec 2007
        • 750

        #4
        I guess you should look into RequiredFieldVa lidator, CustomValidator.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Huh, you're right PRR!

          I don't think I read the title....I think I just read the description of the problem.

          Really the OP should be setting the TextBox.MaxLeng th Property too.

          The thing about client-side validation is that it is quite easy to get get around it. People can even get around the MaxLength if easily they wanted to.

          Please remember always do validation server side before using anything provided by the user.

          -Frinny

          Comment

          • PRR
            Recognized Expert Contributor
            • Dec 2007
            • 750

            #6
            Originally posted by Frinavale
            Huh, you're right PRR!

            I don't think I read the title....I think I just read the description of the problem.

            Really the OP should be setting the TextBox.MaxLeng th Property too.

            The thing about client-side validation is that it is quite easy to get get around it. People can even get around the MaxLength if easily they wanted to.

            Please remember always do validation server side before using anything provided by the user.

            -Frinny
            :) your answers are just amazing and very thorough. You really answer them very well. Keep it up.
            Yeah i guess textbox.maxleng th will do too.

            Comment

            Working...