views:

319

answers:

5

I have a form that I want to create whereby the user enters in one digit per text box. I need all those digits to be correct when the user clicks a button - imagine the iPhone Passcode lock feature.

Ideally I'd like the button only to be clickable when all the digits are correct (perhaps with a variable I define) - otherwise it doesn't perform an action.

HTML -

<form id="journey">
  <input name="code1" type="text" class="code onlyNumeric"  maxlength="1" />
  <input name="code2" type="text" class="code onlyNumeric"  maxlength="1" />
  <input name="code3" type="text" class="code onlyNumeric"  maxlength="1" />
  <input name="code4" type="text" class="code onlyNumeric"  maxlength="1" />
  <input name="code5" type="text" class="code onlyNumeric"  maxlength="1" />

  <input type="button" class="button" value="Next location" />
</form>

At the moment I'm seeing if I can use this with jQuery/Javascript but am unsure how to go about this.

I'd really appreciate some help with this. Thank you.

+1  A: 

You could do something like this. Note this isn't tested but the logic should make sense. isNumeric isn't an actual function btw, so you will need to define your own numeric check.:

$(function() {
$("#journey input").keyup(function() {
    // Fetch all the fields and test that they are all numeric.
    var $code_fields = $("#journey input[class='onlyNumeric']");
    var all_numeric = true;

    $code_fields.each(function(i, el) {
       if (!isNumeric($(el).val())) {
        all_numeric = false;
       }
    });

    // You will need to give your Next Location button some kind of id.
    if (all_numeric == true) {
        $("#next_location_btn").attr("disabled", "false");
    }
});
});

Also, set your "Next Location" button to disabled on page load. You probably want to set that via JS incase the user does not have Javascript enabled so you fall back nicely.

Bartek
A: 
   $(".onlyNumeric").keydown(
        function(event){
            // get the ASCII value for the key that was pressed
            if(event.keyCode < 48 || event.keyCode > 57){
                return false; // don't add the value to the input because it's not 0-9
            }
        }
    );

That should get you there most of the way there. Good luck!

inkedmn
A: 

Hi Bartek,

That does help. I didn't think to disable the button on page load.

To help explain further, I code in place to check if it is only numbers being entered, as well as some hover effects:

$(document).ready(function() {

<!--Form input-->
$('.onlyNumeric').numeric();

<!--Turn the input box focus on and off-->
    $('input[type="text"]').addClass("idleField");
    $('input[type="text"]').focus(function() {
     $(this).removeClass("idleField").addClass("focusField");
     if (this.value == this.defaultValue){ 
      this.value = '';
     }
     if(this.value != this.defaultValue){
      this.select();
     }
    });
    $('input[type="text"]').blur(function() {
     $(this).removeClass("focusField").addClass("idleField");
     if ($.trim(this.value) == ''){
      this.value = (this.defaultValue ? this.defaultValue : '');
     }
    });
});

The onlyNumeric class is being used from a jQuery plugin.

What I am stuck with though is the validation aspect. How do I put in place that say the numbers 1, 2, 3, 4, 5 are the only numbers that cause the button to go to, say, another web page. I imagined setting up a variable that would be checked against this.

Have I explained myself clearly?! :s Thanks.

Liam
A: 

I based my solution off the AlphaNumeric plugin for jQuery. This plugin allows you to do the numeric() functionality, but also extends it by allowing you to do custom overrides to mask certain characters / allow others. In your case, let's say you wanted the following input fields to only accept the numbers 1-5 inclusive:

<form id="journey">
  <input name="code1" type="text" class="code onlyNumeric"  maxlength="1" />
  <input name="code2" type="text" class="code onlyNumeric"  maxlength="1" />
  <input name="code3" type="text" class="code onlyNumeric"  maxlength="1" />
  <input name="code4" type="text" class="code onlyNumeric"  maxlength="1" />
  <input name="code5" type="text" class="code onlyNumeric"  maxlength="1" />

  <input type="button" class="button" value="Next location" />
</form>

The corresponding jQuery code using the above plugin would look like this:

$('#journey input.onlyNumeric').numeric({ichars:'67890'});

Simply: this makes the characters 6,7,8,9 and 0 invalid in that field. This technique could also be used to allow additional characters (like a period for floating point values):

$('#journey input.onlyNumeric').numeric({ichars:'67890', allow:'.'});

Additional documentation can be found on the plugin's website. Of course, remember to not only check for valid input in Javascript, but also on submission, as not all your users might have javascript.

Good luck, sir!

konistehrad
Thanks konistehrad, I see what you're trying to do, prevent certain numbers from being entered, but what I imagined was that when the user submits the form, the text boxes contents are checked against a variable I define, that way, the form is functional, without being hackable you know.Thanks
Liam
That kind of functionality must be implemented on the server after submission, not in Javascript. Any Javascript validation routine will inherently be "hackable", as the user can simply bypass your checks by disabling Javascript in their browsers. Again, you can do usable validation in real time with Javascript, but you must check on the server before utilizing it.
konistehrad
A: 

Use a range validation:

Head Area:

  <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/lib/jquery.delegate.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"&gt;&lt;/script&gt;
<script type="text/javascript">
jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
});;
</script>

<script>
  $(document).ready(function(){
    $("#myform").validate({
  rules: {
    field: {
      required: true,
      range: [13, 23]
    }
  }
});
  });
</script>

Body Content

<form id="myform">
  <label for="field">Required, minium 13, maximum 23: </label>
  <input class="left" id="field" name="field" />
  <br/>
  <input type="submit" value="Validate!" />
</form>
Anthony M. Powers
Cheers Anthony for the suggestion. I have this functionality in place via some different coding, but thank you very much for your suggestion. What I'd like to do is check the textfield's data against a variable that I've defined elsewhere...would you know how to achieve this server side?
Liam
Server-side? This is a javascript conversation! Do you want your form to contact a server-side script to validate those numbers?
Anthony M. Powers
Yes. I was recommended to validate the numbers server side as this prevents users by passing the form altogether if they turn off Javascript in their browser.
Liam