简体   繁体   中英

How to control checkbox value on client side with mvc

I have a problem. How to control checkbox value on client side with mvc, is this possible ? My used checkbox's value getting bool variable. Other guy changed with different value for example,

Original;

<input data-val="true" data-val-required="required place" id="Confirm" name="Confirm" required="required" type="checkbox" value="true">

TryToHack;

<input data-val="differentValue" data-val-required="required place" id="Confirm" name="Confirm" required="required" type="checkbox" value="differentValue">

Such that when throw exception, how to block this state with mvc ?

You can do this with jQuery. Value attribute shouldn't be bool, it can be text.

$('#Confirm').val(); //returns value of your checkbox
$('#Confirm').val("test"); //sets value to "test"
$('#Confirm').is(':checked'); //returns if checkbox is checked (true or false)

You may throw an error based on the attribute value as,

$(document).ready(function(){
    if($("input[required=required]").attr("data-val") != "true")
    {
        throw new Error("Your error message");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM