Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Enter Button on Input

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #163139
    Dext3r
    Participant

    Learning Javascript through an edit i have so im very new to all this still, i was hoping a kind soul could help me out im not completely sure why i cant get a button working on this, as for now it works with Enter Key as you can see, but i also need a button to work with it.

            $("#profile").keydown(function(ev) {
            if(ev.keyCode == 13) {
                if(!$("#profile").val().match(/^[a-zA-Z0-9-_]+$/)) {
                    alert("Invalid name.  Names may contain alphanumeric characters, underscores, and hyphens");
                    return;
                }
                document.location = "r/" + $("#profile").val();
            }
        });
    
    #163328
    Chromawoods
    Participant
    var handleProfileName = function(val) {
    
      if(!val.match(/^[a-zA-Z0-9\-_]+$/)) {
        alert("Invalid name.  Names may contain alphanumeric characters, underscores, and hyphens");
        return;
      }
    
      alert("It is valid.");
    };
    
    
    $(document)
      .on('keydown', '#profile', function(event) { 
        if(event.keyCode === 13) {
          handleProfileName($(this).val());
        }
      })
      .on('click', '#btn-profile', function() { 
        handleProfileName($('#profile').val()); 
      });
    

    Something like that?

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.