Home Validating input field based on radio input and display error message
Reply: 1

Validating input field based on radio input and display error message

Pradvaar cruz
1#
Pradvaar cruz Published in 2017-09-13 19:31:43Z

I'm trying to display a error message using jQuery while submitting the form, when radio button is selected true and input box is empty, for some reason its failing. Any suggestions are appreciated!

<div class="row col-lg-offset-2 top-buffer">
<h4>
    <strong>
        <span>Is Account Deleted?:</span>
    </strong>
</h4>
            @Html.RadioButtonFor(model => model.IsDeleted, true) @Html.Label("Yes")
            @Html.RadioButtonFor(model => model.IsDeleted, false) @Html.Label("No")

<br>
</div>
<div class="row col-lg-offset-2 top-buffer" id="reason">
    <div class="col-sm-2">
        <b>Reason for Delete:</b>
    </div>
    <div class="col-sm-10">
        @Html.TextBoxFor(model => model.reason, new { @class = "form-control", @id = "reason" })

        <br />
    </div>
    <div class="row col-sm-12">
        <label id="lblErrorMsg" class="has-error" style="color: #BD362F"></label>
    </div>
    <div class="col-sm-4">
        <button type = "submit" id="Next" name="Next" class="btn btn-primary">Next</button>
    </div>
</div>

Script:

<script>
$("#Next").click(function () {
    if ($("input[name='IsDeleted']" === "True" && $("#reason").val(''))) {
        $("#lblErrorMsg").text("Please enter meter number");
    } else {
        $("#lblErrorMsg").text("");
    }
});

pegla
2#
pegla Reply to 2017-09-13 19:45:11Z

I can't be sure since I don't see full html code but try instead of:

if ($("input[name='IsDeleted']" === "True" && $("#reason").val(''))) {

this:

if ($("input[name='IsDeleted']:checked").val() === "True" && $("#reason").val() == '')

improved answer a bit, but make sure that IsDeleted radio button has Value "True"

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306792 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO