Laravel 8: Why Login Blade Does Not Show Javascript Alert

I'm working on my login.blade.php and I want to show an alert box if user faced an error while filling the form:

<form method="POST" action="{{ route('login') }}">
    @csrf
    <div class="field">
        <span class="fas fa-user"></span>
        <input type="email" name="email" required>
        <label style="right:0;">Email</label>
        @error('email')
            alert('Something went wrong');
        @enderror                   
    </div>
    <div class="field">
        <span class="fas fa-lock"></span>
        <input type="password">
        <label style="right:0;">Password</label>
        @error('email')
            alert('Something went wrong');
        @enderror
    </div>
    <button>Login</button>
</form>

So as you can I put the alert() if user faced any error with email or password, but the problem is it does not show the pop up message!

I mean it just prints it as text next to input.

So if you know what is wrong here or how can I solve this, please let me know, I would really appreciate any idea from you guys...

2 answers

  • answered 2020-11-25 06:52 Babak Asadzadeh

    i think that's because you forget <script> tag do it like this:

    @error('email')
        <script>
            alert('Something went wrong');
        </script>
    @enderror
    

  • answered 2020-11-25 06:53 Alex

    It shown just as a text because you use it like text, to make it work you need to declare it like a script, like:

    @error('email')
       <script type="text/javascript">
          alert('Something went wrong');
       </script>
    @enderror