Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2014
    Posts
    420
    Thanks
    72
    Thanked 0 Times in 0 Posts

    onFocus/setFocus query

    Code:
    function checkForm()
       {
           var str = ''; //for testing purposes later
           var e = document.getElementsByTagName('input');
     
          //Insert for statement here//
           for(i=0; i <= e.length; i++){
               var type = e[i].getAttribute('type');
               if(type == "text" || type == "textarea"){
                   if(e[i].value.length <1){
                       alert("The field " + e[i].name + " is blank");
                       e[i].focus();
                       return false;
                   }
               }
           }
        }
    I have come across this little script to add focus to a form field in an ASP.NET Web page. The focus should be on a field called 'username'. Would I just replace 'input' in the script above with 'username'? I'm not sure what the reference is there to 'textarea'. There is no text area in my form, just four form fields: username, password, confirm password, and email.

    Thank you.

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,492
    Thanks
    30
    Thanked 889 Times in 887 Posts
    autofocus attribute will do this in html and JS is not needed nor ASP magic.

    Code:
    <form action="demo_form.asp">
      First name: <input type="text" name="fname" autofocus><br>
      Last name: <input type="text" name="lname"><br>
      <input type="submit">
    </form>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. Users who have thanked sunfighter for this post:

    Blue1 (06-06-2014)

  4. #3
    Regular Coder
    Join Date
    Feb 2014
    Posts
    420
    Thanks
    72
    Thanked 0 Times in 0 Posts
    Thank you for that, sunfighter.

    I thought JS was necessary!

    Learn something every day!

    Cheers

  5. #4
    Regular Coder
    Join Date
    Feb 2014
    Posts
    420
    Thanks
    72
    Thanked 0 Times in 0 Posts
    Hello sunfighter

    Just for your records or should anyone else ask, the correct script (ASP.NET/Visual Studio 2013) is as follows:

    Code:
     Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs)
            username.Focus()
        End Sub
    where 'username' is ID of the field.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •