0

I am using Django and try to submit a form. I have a "call-us" form and there is 3 fields. I want to make this, If one of the fields is empty, and the user clicked on Submit button, don't send the info to View and warm the user that they must complete the required fields.

here is my form:

<form role="form" action="{% url "landingpages:callusthanks" %}" method="post" style="max-width: 500px;margin: 0 auto;margin-top: 30px;">
    {% csrf_token %}
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-addon"><i class="fa fa-user"></i></div>
            <input type="text"  name="name" class="form-control" id="name" placeholder="Name">
        </div>
    </div>
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-addon"><i class="fa fa-at"></i></div>
            <input type="text" name="email" id="email" class="form-control" placeholder="Email">
        </div>
    </div>
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-addon"><i class="fa fa-envelope-o"></i></div>
            <textarea name="message" class="form-control" id="message" placeholder="Message"></textarea>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-xs-6 col-xs-offset-6">
            <button type="submit" class="form-control">Send</button>
        </div>
    </div>
</form>

1 Answer 1

1

As one option you can use jQuery validation and set the required fields like so:

$(document).ready(function(){
    $("#your_form_id").validate({
       rules :{
            your_field : {
            required : true
            }
            .....
       },
       messages :{
            your_field : {
            required : 'your_field is required'
            }
            .....
       }
    });
});

Edit: Just saw you said not to send to view. So, ignore this but I'll leave it for future reference on the off chance that it's useful.

Preferably, you could turn this into a form import it from forms.py and then send it to your view. You could then just set which fields are required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.