8

I want to make a "NumPad" where I can type in a 4-digit-keycode in an input field. After I click the four buttons, the submit-button gets pressed. I found a few code-snippets and it worked if I use the keyboard. This is how far I come:

function addNum(num){
    document.getElementById('login').value += num;
}
$('#login').keyup(function(){
    if(this.value.length == 4){
        $('#enter').click();
    }
});
#numpad {
  width: 200px;
}

.row {
  width: 100%;
}

.number {
  min-width: 26%;
  height: 60px;
  float: left;
  border: 1px solid;
  margin: 10px;
  vertical-align: middle;
  display: block;
  font-size: 2em;
  padding-top: 8px;
  padding-left: 30px;
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  border-radius: 15px;
}

a:hover .number {
  background: black;
  color: white;
}
<form action="action.php" method="post" name="loginform" id="loginform">
  <input type="password" class="form-control" name="login" id="login">
  <input type="submit" id="enter" value="Submit">
</form>
<div id="numpad">
  <div class="row">
    <a href="#" onClick="addNum('1');"><div class="number">1</div></a>
    <a href="#" onClick="addNum('2'); return false"><div class="number">2</div></a>
    <a href="#" onClick="addNum('3'); return false"><div class="number">3</div></a>
  </div>
  <div class="row">
    <a href="#" onClick="addNum('4'); return false"><div class="number">4</div></a>
    <a href="#" onClick="addNum('5'); return false"><div class="number">5</div></a>
    <a href="#" onClick="addNum('6'); return false"><div class="number">6</div></a>
  </div><div class="row">
    <a href="#" onClick="addNum('7'); return false"><div class="number">7</div></a>
    <a href="#" onClick="addNum('8'); return false"><div class="number">8</div></a>
    <a href="#" onClick="addNum('9'); return false"><div class="number">9</div></a>
  </div>
</div>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Daniel
  • 113
  • 1
  • 7
  • You forgot to include jQuery in the Stack Snippet. There’s a dropdown menu to select jQuery. – Sebastian Simon Apr 13 '16 at 15:26
  • 1
    Try .change() instead of .keyup(). – Lewis Apr 13 '16 at 15:28
  • An alternative would be to hook up to the `change` event, and if the value is a 4-digit number, the form gets submitted. The RegEx to identify a 4-digit number is simply: `^\d{4}$` – Ted Nyberg Apr 13 '16 at 15:29
  • check [this answer](http://stackoverflow.com/questions/36284804/move-data-entry-to-the-next-field-when-one-is-full-js-tabulation/36285086) for a better experience and also [this fiddle](http://jsfiddle.net/hjsv8dt5/) – Daniel Almeida Apr 13 '16 at 15:37

2 Answers2

6

Add your submit check to your addNum() function:

function addNum(num)
{
    document.getElementById('login').value += num;
    if((document.getElementById('login').value.length == 4) && document.getElementById('login').value.match(/^\d{4}$/))
    {
        alert("Form Submitted");
        document.forms["loginform"].submit();
    }
}
$('#login').keyup(function() {
    if((this.value.length == 4) && document.getElementById('login').value.match(/^\d{4}$/))
    {
        alert("Form Submitted");
        document.forms["loginform"].submit();
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="action.php" method="post" name="loginform" id="loginform">
  <input type="password" class="form-control" name="login" id="login">
  <input type="submit" id="enter" value="Submit">
</form>
<div id="numpad">
  <div class="row">
    <a href="#" onClick="addNum('1');"><div class="number">1</div></a>
    <a href="#" onClick="addNum('2');"><div class="number">2</div></a>
    <a href="#" onClick="addNum('3');"><div class="number">3</div></a>
  </div>
  <div class="row">
    <a href="#" onClick="addNum('4');"><div class="number">4</div></a>
    <a href="#" onClick="addNum('5');"><div class="number">5</div></a>
    <a href="#" onClick="addNum('6');"><div class="number">6</div></a>
  </div><div class="row">
    <a href="#" onClick="addNum('7');"><div class="number">7</div></a>
    <a href="#" onClick="addNum('8');"><div class="number">8</div></a>
    <a href="#" onClick="addNum('9');"><div class="number">9</div></a>
  </div>
</div>

You may want to validate that it is actually a 4-digit number using regex:

document.getElementById('login').value.match(/^\d{4}$)

The form submission is blocked here that is why I put the alert in...

brso05
  • 13,142
  • 2
  • 21
  • 40
  • 1
    Matching regex `^\d{4}$` would make this more robust I think, as opposed to simply checking if the value is 4 characters long. – Ted Nyberg Apr 13 '16 at 15:31
  • @brso05 +1 My mistake, that'll teach me not to read. I'll delete my comments, they are pointless. – Lewis Apr 13 '16 at 15:41
  • 1
    @Daniel I had the `regex` wrong originally...I had `d{4}` instead of `^\d{4}$`. It is updated now and should work. – brso05 Apr 13 '16 at 16:01
3

You can clean this up a fair bit by replacing your inline click events with a listener:

$(function(){
    $('#numpad a').on("click",function(e){//listen for clicks on the numpad elements
        e.preventDefault();//don't follow the link
        $('#login').val($('#login').val()+$(this).text()).change();//append the value
    });
    $('#login').on("change keyup",function(){//on change or keyup
        if($(this).val().length == 4){//if the val is 4 chars long
            //$('#loginform').submit();//trigger submission
            $('#enter').click();//or click the submit button if you dont want to bypass the browsers native validation
        }
    });
    
    $('#loginform').on("submit",function(e){//on submit            
        if($(this)[0].checkValidity()){//if valid
          e.preventDefault();//stop submission for this demo only (remove when live)
          alert("submitted!");//do an alert (demo only, remove when live)
        } else {
          e.preventDefault();//prevent submission
          alert("Invalid!");//do an alert
        }
        $('#login').val("");//reset the input
    });
});
#numpad {
  width: 250px;
  -webkit-user-select: none; /* Chrome/Safari */        
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none; /* IE10+ */
}
#numpad a {
  width: 60px;
  line-height: 60px;
  display:inline-block;
  border: 1px solid;
  margin: 10px;
  vertical-align: middle;
  font-size: 2em;
  text-align:center;
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  border-radius: 15px;
}
#numpad a:hover {
  background: black;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="action.php" method="post" name="loginform" id="loginform">
  <input type="password" class="form-control" name="login" id="login" required="required" pattern="[0-9]{4}" title="4 numbers only" maxlength="4" />
  <input type="submit" id="enter" value="Submit" />
</form>
<div id="numpad"><a>1</a><a>2</a><a>3</a><a>4</a><a>5</a><a>6</a><a>7</a><a>8</a><a>9</a><a>0</a></div>
Moob
  • 14,420
  • 1
  • 34
  • 47