Php

Laravel 9 Form Validation Client-Side Using jQuery

This tutorial help to add client-side validation into laravel form. I have already shared Laravel 5.6 Server Side Validation Example Using Resource Controller.

I am using jquery and JavaScript to validate laravel form client side. You can use any third-party javascript libs as well to validate laravel form, But I am creating my own method to validate form input fields and display message.

Laravel 9 Form Validation

We have created add employee form in Previous Tutorial, Now we will create a generalized javascript method to validate form.

function validateForm(type) {
    let formname = $('#Employee-'+type);
    let isValidate = true;
    formname.find(".require-field").each(function(index, el) {
        let ele_name = $(el).attr('name');

        if ($(el).val() == "") {
            isValidate = false;
            $(el).parent('div.form-group').addClass('has-error');

           $('div[name='+ele_name+'-error-span]').html('The template '+ele_name+' is required.');
        }
    });
    console.log(isValidate);
    if(!isValidate) {
        return false;
    }
}

The above method will validate add and edit form page, We just pass the form type as a parameter(add or edit).

I am finding all required fields of forms and checking the value if the input field is required and the value is empty then displaying the validation message otherwise return true(all validation rule has been passed).

We will modify and add employee form information as per the above method.

Related Post

modified form id #Employee-add and called validateForm() method on onsubmit form event.

Now add '.require-field' class within form element, that tells JavaScript validation method – it’s a required field and validate.

<div class="form-group {{ $errors->has('name') ? ' has-error' : '' }}"><label class="control-label">Employee Name</label> <input class="form-control require-field" name="name" type="text" value="{{Request::old('name')}}"> @if ($errors-&gt;has('name')) <span class="help-block"><strong>{{ $errors-&gt;first('name') }}</strong></span> @endif
<p>&nbsp;</p>
<div>&nbsp;</div>
</div>

I have also associated one more div ‘name-error-span’ with each required input field, This div will use to display the validation messages.

I also added removeErrors() method to hide validation failed message on keyup input, if the user has been entered some values into the input field.

The above code will validate laravel form and display message but not hide the display message, Let’s create removeErrors() method to hide message.

function removeErrors(el){
       ele_name = $(el).attr('name');
       
        if ($(el).val() != "") {
            $(el).parent('div.form-group').removeClass('has-error');
            $('div[name='+ele_name+'-error-span]').hide();
        } else {
            $(el).parent('div.form-group').addClass('has-error');
            $('div[name='+ele_name+'-error-span]').show();
        }
}

Conclusion:

We have created laravel form using a blade template. I have also added client-side validation using jquery to validate form inputs. The validation message is displayed in the span using the bootstrap css framework.

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories