0

I am using the jQuery.steps Plugin and do have now some problems regarding the validation, I have a field "username" which has to be validated (if the username already exists) before continuing to the next step, let me show you some code:

removed

The PHP code looks like this

removed

the issue I have here is that when I click on the next button the form just continues even if the username is already taken.. if I click on back and the username was already taken the error message is displayed and I am not able to continue before choosing an available username.

When I do enter for example 7 characters in the username field and click on next the form does not continue, after removing the 7 charakter and enter a username that is already taken the form does also not continue.

I hope you guys do unterstand my issue and do have any great solution (as always) for me :-)

Please let me know if you need any additional information.

Cheers!

UPDATE Since my problem was not really clear, I created a small online example which should reflect my issue, you can find that here: http://example.crasher.ch/

There sources used on this page Looks like this

index.php

<form id="test" name="test" action="#" style="width: 650px">
        <h3>Personelles</h3>
        <div>
            <p>
                Lorem ipsum dolor sit amet.
            </p>
            <label for="name">Name</label><input type="text" id="bva_name" name="bva_name" value="Name">
            <br /><br class="clear" />
            <label for="vorname">Firstname</label><input type="text" id="vorname" name="vorname" value="Firstname">
            <br /><br class="clear" />
            <label for="username">Username</label><input type="text" id="username" name="username" value="abcdef">
            <br /><br class="clear" />
        </div>

        <h3>Arbeitsplatz</h3>
        <div class="arbeitsplatz">
            <p>
                Lorem ipsum dolor sit amet, consetetur sadipscing elitr.
            </p>
            <label for="something">Something</label>
            <input type="text" name="something" id="something" />
            <br /><br class="clear" />
        </div>

        <h3>Abschluss</h3>
        <div>
            <p>
                Lorem ipsum dolor sit amet, consetetur sadipscing elitr.
            </p>
            <label for="otherlogins">Sonstige Logins</label>
            <textarea name="otherlogins" id="otherlogins"></textarea>
            <br /><br class="clear" />
        </div>
    </form>
    <!-- JQUERY FUNCTIONS -->
    <script>
        $(function()
        {
            // initialize wizzard form  
            var form = $("#test").show();

            form.steps({
                headerTag: "h3",
                bodyTag: "div",
                transitionEffect: "slideLeft",
                onStepChanging: function (event, currentIndex, newIndex)
                {
                    // Allways allow previous action even if the current form is not valid!
                    if (currentIndex > newIndex)
                    {
                        return true;
                    }
                    // Needed in some cases if the user went back (clean up)
                    if (currentIndex < newIndex)
                    {
                        // To remove error styles
                        form.find(".body:eq(" + newIndex + ") label.error").remove();
                        form.find(".body:eq(" + newIndex + ") .error").removeClass("error");
                    }

                    form.validate().settings.ignore = ":disabled,:hidden";
                    return form.valid();
                },
                onFinishing: function (event, currentIndex)
                {
                    form.validate().settings.ignore = ":disabled,:hidden";
                    return form.valid();
                },
                onFinished: function (event, currentIndex)
                {
                    alert('you did it');
                },
                labels: 
                {
                    cancel: "Abbrechen",
                    current: "Aktueller Schritt:",
                    pagination: "Pagination",
                    finish: "Finish",
                    next: "Next",
                    previous: "Back",
                    loading: "Lade..."
                }
                })
                .validate({
                    rules: {
                        name: {
                            required: true,
                            minlength: 2
                        },
                        vorname: {
                            required: true,
                            minlength: 2
                        },
                        username: {
                            required: true,
                            minlength: 6,
                            maxlength: 6,
                            remote: {
                                url: "ajax.php",
                                type: "POST",
                                data: {
                                    action: function() {
                                        return "validateUsername";
                                    },
                                    username: function() {
                                        return $("#username").val();
                                    }
                                }
                            }
                        }
                    }
            });
        });
    </script>
    <!-- /JQUERY FUNCTIONS -->

ajax.php

<?php
$username = $_POST['username'];

if ( $username == "abcdef" || $username == "qwertz" )
{
    echo json_encode("Der Benutzername ist bereits vergeben");
}
else
{
    echo "true";
}
?>

as you can see if you just click on next the validation does not work even if the prefilled username is not allowed, when you click on the back button you can see the error message and can not continue until an other username has been entered. also if you change any field (for example make the firstname empty) click on next the username is also validated correctly since the firstname triggered the validation - I hope now my issue is clear to everyone :)

Crasher
  • 113
  • 2
  • 12
  • Within your `remote` options, get rid of the `data` parameter entirely. The value of the field being validated is already sent by default. As long as the PHP is responding with an `echo 'true'` or `echo 'false'` for pass/fail the validation plugin will pick this up. If your PHP responds with a string when validation fails, then it must respond with `echo json_encode('your string')` and this string will automatically become the validation message. – Sparky Nov 10 '15 at 16:05
  • Since `remote` validation depends on what your server-side code is sending back, it would be a good idea to show us that too. – Sparky Nov 10 '15 at 16:12
  • Do not put code into a comment. Please click the "edit" link and add it to your OP. – Sparky Nov 10 '15 at 16:16
  • Your PHP code is the problem. Please re-read my first comment and review this: http://stackoverflow.com/questions/16577120/jquery-validate-remote-method-usage-to-check-if-username-already-exists – Sparky Nov 10 '15 at 16:18
  • @Sparky just did that – Crasher Nov 10 '15 at 16:18
  • 1. Remove `data` parameter from `remote` options. 2. From PHP, only `echo "true"`, `echo "false"`, or `echo json_encode("string")`. – Sparky Nov 10 '15 at 16:20
  • I changed the php part as you said, but that did not solve the problem.. If I remove the data paramater how will the "action" be sent to the php file? – Crasher Nov 10 '15 at 16:22
  • You do not need the `action`. The jQuery Validate plugin automatically picks up the `echo` response from the PHP. Again see: http://stackoverflow.com/questions/16577120/jquery-validate-remote-method-usage-to-check-if-username-already-exists – Sparky Nov 10 '15 at 16:23
  • `echo json_encode("Der Benutzername ist bereits vergeben");` would be the response on failure. – Sparky Nov 10 '15 at 16:25
  • I checked to link you provided - the php file as many functions depending on which action is posted to the file, so I do need to send an action. The validation works great but just not the first time.. – Crasher Nov 10 '15 at 16:25
  • So what happens when you leave the `action`? – Sparky Nov 10 '15 at 16:27
  • Exactly the same, I think the issue is related to the combination of the remote validation and the way the jquery.steps handles the validation while clicking on next – Crasher Nov 10 '15 at 17:03
  • I also don't think you can chain `.validate()` to the `.steps()` method. You're trying to use `.valid()` within `.steps()` before the Validate plugin has been initialized. Separate the methods and call `.validate()` first. – Sparky Nov 10 '15 at 17:12
  • Based on the advanced form example from the jquery steps webpage they way I do it should work, so can you please remove the duplicate entry? – Crasher Nov 10 '15 at 17:27
  • Is all the other validation working except for `remote`? If so, then it's an issue with `remote`. Otherwise, please edit the question to more accurately reflect the issue and I'll review for re-opening. Also include the relevant HTML so a demo can be created. – Sparky Nov 10 '15 at 17:33
  • Also, have you done any troubleshooting using your console to see exactly what the `ajax` is doing? Are you positive the PHP function is being invoked? What is the server response to the `remote` call? – Sparky Nov 10 '15 at 17:39
  • Of Course, Validation works without any issues if there is no remote involved. And for sure I did the Troubleshooting the PHP function is called and works - as I tried to explain as soon something is validated (not remote) for example more than 6 characters in the username field, the next time also the remote Validation works without any issues - I will try to create an example online for you guys. – Crasher Nov 10 '15 at 18:37
  • @Sparky I updated my original post with a link to an example and a new description – Crasher Nov 10 '15 at 19:25
  • Your online demo is using jQuery Validate version 1.9... you know that version is more than 4 years old. Update the plugin to 1.14. According to the developer, it's never been tested with jQuery v2. – Sparky Nov 11 '15 at 16:04
  • Updated just now to the new version, as requested, everything is still the same - again I am pretty sure it is a problem with the compination of the steps and the validation - so would you please remove the duplicate, since it is not. – Crasher Nov 11 '15 at 19:11
  • I removed the duplicate. However, if you load the page without the username already filled out, it does not demonstrate the problem you describe. – Sparky Nov 11 '15 at 19:21
  • Thanks, you are right - but in my case the username is already filled out – Crasher Nov 11 '15 at 19:39
  • This is a huge clue to solving your problem. Validation is triggered on every keyup, when you blur, and upon clicking the submit. However, since you're using `remote`, it's not triggered fast enough when the button is clicked. You need a handler to force validation on this field immediately. Perhaps when you load this tab/page, you should call `$('input[name="username"]').valid()`. – Sparky Nov 11 '15 at 19:57
  • I see your point and had the same idea before unfortunatly without success.. let me give you an example which does not confirm this theory: directly after the page is loaded, enter the username field, remove the "f" at the end and then add for example an "s" remove the "s" again and add the "f" click on next and there is still no validation running - but there where many events which should already have triggered the validation. But if you click directly next and then back, each change in the username field triggers a validation immediately. – Crasher Nov 11 '15 at 22:20
  • That's because, by default, validation with this plugin is "lazy". So there is no `keyup` validation at all when the page first loads. It takes an initial submit to trigger validation before validation becomes "eager". Since your form is completely filled out already, validation "appears" to be satisfied when the button is clicked and `remote` is never invoked. There are some nuances to this such as what happens when a required field is left blank. – Sparky Nov 11 '15 at 23:14
  • Do you see a way to simulate an initial submit? So the form is directly validated after loading the page? – Crasher Nov 12 '15 at 09:30
  • I "solved" the issue just now by adding `form.valid();` at the end of the jQuery code I think that does the trick or do you think there is any problem with this? – Crasher Nov 12 '15 at 10:00
  • That's a very effective way to eliminate the "lazy" validation. As long as you don't have a problem with validation messages appearing immediately on page load, I think you've solved it. – Sparky Nov 12 '15 at 13:29

0 Answers0