How to display an error message in a page when some action is wrong

Hi

I have a page in which a user is doing some data search.

I want the form to remain as it is, in case the user does not fill in some fields. And an error message should be shown as well.

How do I do this?

Thanks,
Arvind.

  • Jack Kitterhing

    Hi there @Arvind

    I hope you are well today, could I have a bit more information about the form please? Is this a PHP form where the data will be checked and stored in the database, does it having any Javascript or jQuery elements?

    How do you want to add the error message? Do you have a link to a pastebin with your current form code?

    Please advise :slight_smile:

    Thank you!

    Kind Regards
    Jack.

  • Arvind

    Hi

    I tried using JQuery Validator plugin. That allows simple rules to be defined for validation of fields in a form.

    However that is also not working.

    THis is the code I am using

    <script>

    jQuery.validator.addMethod("val_address", function(value, element) {
    return (value == '<?php echo $zillow_option['zillowaddress']; ?>');
    }, "Address must be set to a value other than default...");

    jQuery.validator.addMethod("val_city", function(value, element) {
    return (value == '<?php echo $zillow_option['zillowcity']; ?>');
    }, "City must be set to a value other than default...");

    jQuery.validator.addMethod("val_zip", function(value, element) {
    return (value == '<?php echo $zillow_option['zillowzip']; ?>');
    }, "Zip must be set to a value other than default...");

    jQuery.validator.addMethod("val_state", function(value, element) {
    return (value == '<?php echo $zillow_option['zillowstate']; ?>');
    }, "State must be set to a value other than default...");
    $.validator.setDefaults({
    submitHandler: function () { alert("submitted!"); }
    });

    var ruleSetaddress = {
    val_address : true,
    required: true

    };

    var ruleSetcity = {
    val_city : true,
    required: true

    };

    var ruleSetstate = {
    val_state : true,
    required: true

    };

    var ruleSetzip = {
    val_zip : true,
    required: true

    };

    $("#advanced-searchform").validate({
    rules: {
    search_address: ruleSetaddress,
    search_city: ruleSetcity,
    search_state: ruleSetstate,
    search_zip: ruleSetzip

    },
    errorElement: "span" ,
    messages: {
    search_address: " Enter Address",
    search_city: " Enter City",
    search_state: " Enter State",
    search_zip: " Enter Zip",

    }
    });

    $("#searchform").validate({
    rules: {
    search_address: ruleSetaddress,
    search_city: ruleSetcity,
    search_state: ruleSetstate,
    search_zip: ruleSetzip

    },
    errorElement: "span" ,
    messages: {
    search_address: " Enter Address",
    search_city: " Enter City",
    search_state: " Enter State",
    search_zip: " Enter Zip",

    }
    });

    </script>
    For some reason, nothing is happening- the values obtained using PHP code are basically shown as the default values. So the rules are to check if the default values are being submitted unchanged (which should not be done) and to check if blank values are being sent(again this should not be done).

    What is wrong with above code?

    Or, if you can suggest simpler solution for above problem- that is also acceptable.

    Yours sincerely,
    Arvind.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.