2

i'm looking for a solution to validate the input of a textfield against an array which is provided by a json-file. For example: To check within a registration form, if the desired username already exists.

Can anybody help out with a working example, plugin, script, or tutorial?

The 'usernames.json' looks like this...

{"usernames":["carl","jack","jill"]}

...and is read out with...

$.getJSON("usernames.json", function(names) {
var invalidName = names;

I would go on with...

jQuery.validator.addMethod

...but from here on, i'm stuck.

If anybody may help out with a working example, i'd be very glad!

////////////////////////////////////////////////////

EDIT: Thanks to Utkanos and Pavel Staseljun, i could figure out the following solution, which is only roughly tested! Please also find Utkanos' example down below, because this one includes "keyup"-handling instead of "submit" and does not include a "required"-check. "#errorresponse" is the div, where an error-message may be displayed.

$.ajax({
  url: "http://yoururlto.json?callback=?",
  dataType:"jsonp",
  jsonpCallback: '?',
  async: false,
  success: function(json) {
        $('#user').keyup(function(evt) {
        var jsonarray = json.usernames;
        var userinput = $('#user').val().toLowerCase()
        var check = $.inArray(userinput, jsonarray);
        if (check !== -1) {
            $("#errorresponse").text('Thanks to Utkanos and Pavel Staseljun!');
            }
        })
      }
});

3 Answers 3

1

HTML

<form id='signup'>
    <label>Choose a username</label>
    <input type='text' id='user' />
    <input type='submit' value='submit' />
</form>

JS / jQuery

$.getJSON("usernames.json", function(json) {
    $('#signup').on('submit', function(evt) {
        var user = $('#user').val(), error;
        if (!user)
            error = 'no username entered';
        else if (json.usernames.indexOf(user) != -1)
            error = 'username already taken';
        if (error) { evt.preventDefault(); alert(error); }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I think if ($.inArray($('#username').val(), json.usernames)) error = 'username already taken'; instead of if (json.usernames.indexOf($('#username').val()) != -1) error = 'username already taken'; is better..?
Well, yes, it's more cross-browser. I just like ECMA5 :)
0

Why do you want to send all usernames from the server to a client? This sounds like a pretty bad idea. Please do something like a server side function which checks for a posted username and returns true or false for valid or invalid?

3 Comments

Because i would prefer some kind of "on the fly" validation, therefore, json is faster. This is supposed to work on mobile devices as well; a server-side function (like php) is impossible in this case.
by using ajax you could use "on the fly" validation and especially on mobile devices you'll get pretty fast to a point where holding an array of 10000 usernames is much more slower than a simple ajax request. In addition it seems to be a security issue if every client can read all your usernames.
Maybe we have a misunderstanding? I do use ajax/js for the validation itself. :-) In fact, i've chosen the "username"-scenario as an example, to keep the question more common. There are two validation steps: One before the form is send (client-side) and another, when the submission is processed via php (server-side). So, in this case, it's just about the fast client-side solution.
0

Can't you create an ajax script checking if a username exists

Much better imo, because you can directly integrate it with jquery validator using remote option.

1 Comment

You're right, this may be one solution, but in this case, i was looking for a client-side "pre-validation".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.