Support the ongoing development of Laravel.io →
Configuration Authentication Validation
Last updated 2 years ago.
0

You can use jquery validator

Last updated 2 years ago.
0

Hi tuyenlaptrinh,

Thanks for replying so quickly.

I am wanting to stay in Laravel and not use any further plugins. After a little more digging, I came up with the following:


<div class="form-group">
	{{ Form::checkbox('agree', 1, null) }}
	{{ Form::label('agree', '&nbsp;I agree to the Terms &amp; Conditions')}}
	{{ $errors->first('agree') }}
</div>

However, I don't know what to do next? Can anyone help?

I want to force users to check the box and add a link to the text 'Terms & Conditions'.

With kind regards,

Mark

Last updated 2 years ago.
0

This is my rule for register and it work for me.

Model

public static $register_rules = [
		'fullname'=> 'required',
		'email' => 'required|email',
		'password' => 'required|min:3|max:20',
		'repassword' => 'required|min:3|max:20',
		'terms' => 'required' // This is check checkbox is checked
	];
Last updated 2 years ago.
0

Thanks again for your quick reply.

However, I don't want to have to rebuild the page from scratch. I just want to add the checkbox and ensure that it has been ticked before the form may be submitted. Is there a way of doing that?

With thanks in advance,

Mark

Last updated 2 years ago.
0

You can use vanilla JS (in addition to server-side validation):

<script type=text/javascript>
function validate(){
    var agreeBox = document.getElementById('agree');
    if (!agreeBox.checked){
        alert('You must agree!');
        return false;
    }
}
</script>

When opening your form, call the validator onSubmit

{{ Form::open(['route' => 'users.store', 'onSubmit' => 'validate()']) }}
Last updated 2 years ago.
0

Please please, with jquery validator it ok. or follow aowie1

Last updated 2 years ago.
0

Thanks aowie1 & tuyenlaptrinh for your help.

I feel that I am almost there.

I modified the code at the start of the form as was suggested, so it read:

{{ Form::open(['route' => 'users.store', 'onSubmit' =>'checkForm(this)']) }}

But, I couldn't get the Javascript to work.

I found this elsewhere:

<script type=text/javascript>
function checkForm(form)
	{
		if(!form.agree.checked) {
		alert("Please indicate that you accept the Terms and Conditions");
		form.agree.focus();
		return false;
		}
	return true;
	}
</script>

I added the script before the form.

When I left the box unticked, the correct message popped up, but on clicking OK in the box, the form was submitted anyway. If I ticked the box, the form posted to users.store as expected, but the message "Whoops, looks like something went wrong" comes up on an otherwise empty screen.

Both of you have helped a lot. But, I am VERY new to all this and think I may need a little more hand-holding!

With kind regards,

Mark

Last updated 2 years ago.
0

@Rwthwyn: Change your code like this and it will work:

{{ Form::open(['route' => 'users.store', 'onSubmit' =>'return checkForm(this)']) }}

Notice the "return" while calling your onSubmit event, this is important :)

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Rwthwyn rwthwyn Joined 5 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.