0

Similar to the following question (note the following question is related to symfony 2.3 - I don't know what that is, I'm just using HTML) :

How to disable html5-validation for specific buttons in symfony 2.3

Is it possible to bypass HTML5 validation (required) for a specific button in HTML?

<form name="loginform" autocomplete="off" id="loginform" action="" method="post">
            <nav class="clearfix">
                <ul class="clearfix">
                    <li><input class="username" type="text" id="username" required placeholder="Username"></li>
                    <li><input class="password" type="password" id="password" required placeholder="Password"></li>
                    <li><button name="register" class="register">Register</button>
                    <button name="login" class="login">Log in</button></li>
                </ul>
                <a href="#" id="pull">Menu</a>
            </nav>
    </form>

I don't need validation if register button is clicked.

if(isset($_POST['register'])) {
    header("Location: register.php");
}

Edit: A way of doing this without changing the structure of the form or changing the button :).

Community
  • 1
  • 1
Abdul Sadik Yalcin
  • 1,744
  • 2
  • 19
  • 50
  • how bout removing the other buttons on clicking the register button? OR have the register button in a different formset? – atmd Feb 04 '15 at 22:34
  • I didn't get the first part of your comment... I have already made tons of styling, don't want to change my structure. It would be good to know if there is a way... I can just change the button to a anchor link... – Abdul Sadik Yalcin Feb 04 '15 at 22:36
  • There are many ways of doing this depending on how you want to proceed: take the button out of the `form`, turn the button into an anchor, pre-process the buttons with JS... – Alvaro Montoro Feb 04 '15 at 22:43
  • I am aware that I can just change to button to an anchor link like I stated on my previous comment but I want to know if there is a way without changing the structure of the form or the button. – Abdul Sadik Yalcin Feb 04 '15 at 22:47
  • I don't generally work with symfony - but is there any reason you can't just remove the `name` attribute on the button and be fine? – Jhecht Feb 04 '15 at 22:52
  • I'm not working with symfony either, you must of misread the question :) – Abdul Sadik Yalcin Feb 04 '15 at 23:28

2 Answers2

1

Add a click event listener to the register button. When it is clicked, do whatever you want to do (redirect with window.location = "....."), and return false to prevent default submission:

$("#register").on("click", function() {
    alert("I am executed before the HTML5 check!");
    return false;
});

You can see an example here: http://jsfiddle.net/89z4Lu91/

Alvaro Montoro
  • 28,081
  • 7
  • 57
  • 86
  • 2
    Use the `formnovalidate` attribute. Look at this example: http://jsfiddle.net/89z4Lu91/1/ – Alvaro Montoro Feb 04 '15 at 23:17
  • 1
    BTW, the `formnovalidate` solution came from here: http://stackoverflow.com/questions/18725078/bypass-html-required-attribute-when-submitting (you can find a good explanation and sources there) – Alvaro Montoro Feb 04 '15 at 23:17
  • Hah! You are a star! I did see that somewhere before I asked the question but it was only for the whole form... Excellent! – Abdul Sadik Yalcin Feb 04 '15 at 23:26
0

To the register button only use <a href="bla">register</a> or put the botons out and to the inputs submit add form attr

MikeStack
  • 29
  • 3