Search code examples
phprequired

required message not a alert in the javascript


How can i write something a message not a alert when the input is empty.

Here in my code it is not preventing to submit the value when it is null, but how can i show a message like an required message that you need to insert something to search

if ( $( "#nameSearch" ).val().length === 0 ) {
    // some message that please insert something to search not an alert
    // required data-fv-notempty-message="This is required"
    event.preventDefault();
    } else {

Solution

  • <form method="post" onsubmit="return validate()">
    <span class="error" id="name-error"></span>
    <input type="text" name="nameSearch" id="nameSearch" >
    <input type="submit" name="submit" value="submit" >
    </form>
    
    function validate()
    {
        if ( $( "#nameSearch" ).val().length === 0 ) {
            $("#name-error").html("This is required");
            return false;
        } else {
            return true;
        }
    }