conmen conmen - 10 months ago 72
jQuery Question

Jquery form submit to check empty fields

How could I use jquery to check if text-fields are empty when submit without loading


<form action="login.php" method="post">
<label>Login Name:</label>
<input type="text" name="email" id="log" />
<input type="password" name="password" id="pwd" />
<input type="submit" name="submit" value="Login" />


Answer Source

You can do this:

//  Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {

    // Get the Login Name value and trim it
    var name = $.trim($('#log').val());

    // Check if empty of not
    if (name  === '') {
        alert('Text-field is empty.');
        return false;