logo
Tags down

shadow

ASP.Net Custom Client-Side Validation


By : John Turnbull
Date : July 27 2020, 02:00 PM
Does that help Try changing the argument names to sender and args. And, after you have it working, switch the call over to ScriptManager.RegisterClientScriptBlock, regardless of AJAX use.
code :


Share : facebook icon twitter icon

ASP.NET Custom Validator Client side & Server Side validation not firing


By : harrije
Date : March 29 2020, 07:55 AM
To fix this issue Your CustomValidator will only fire when the TextBox isn't empty.
If you need to ensure that it's not empty then you'll need a RequiredFieldValidator too.
code :
function TextBoxDCountyClient(sender, args) {
    var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
    if (v == '') {
        args.IsValid = false;  // field is empty
    }
    else {
        // do your other validation tests here...
    }
}
protected void TextBoxDTownCity_Validate(
    object source, ServerValidateEventArgs args)
{
    string v = TextBoxDTownCity.Text;
    if (v == string.Empty)
    {
        args.IsValid = false;  // field is empty
    }
    else
    {
        // do your other validation tests here...
    }
}

Client side validation for custom annotation Asp.Net MVC 4


By : praneeth
Date : March 29 2020, 07:55 AM
it helps some times That article is specific to MVC 2 which used MicrosoftAjax. MVC 4 no longer includes the MS Ajax files as they have been deprecated and the preferred method is to use jquery.
To verify your settings, make sure these scripts are in your layout
code :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
public class MyModel 
{
    [Required]
    [StringLength(30)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(30)]
    public string LastName { get; set; }
}
<div>
    @Html.LabelFor(model => model.FirstName)
</div>
<div>
    @Html.TextBoxFor(model => model.FirstName) <br/>
    @Html.ValidationMessageFor(model => model.FirstName)
</div>

<div>
    @Html.LabelFor(model => model.LastName)
</div>
<div>
    @Html.TextBoxFor(model => model.LastName) <br/>
    @Html.ValidationMessageFor(model => model.LastName)
</div>
$("document").ready(function () {

    var isCustomRateRequired = document.getElementById("IsCustomRateRequired");

    isCustomRateRequired.onchange = function () {
        if (this.checked) {
            $('#Rate').attr('disabled', 'disabled');
            $('#Rate').val('');
        }
        else {
            $('#Rate').removeAttr('disabled');
        }
    };
});

jQuery.validator.addMethod("raterequiredifcustomindexrate", function (value, element, param) {
    var rateRequired = $("#CustomRateRequired").val();
    if (rateRequired && value == "") {
        return false;
    }
    return true;
});

jQuery.validator.unobtrusive.adapters.addBool("raterequiredifcustomindexrate");

Custom client-side ASP.NET MVC 3 Validation


By : Đinh Quang Trung
Date : March 29 2020, 07:55 AM
I hope this helps . I'd like to change the default behavior of the client-side validation that ASP.NET MVC 3 uses in this way: , Ok the answer was REALLY simple..
I just added this CSS rule:
code :
.custom-validation-error  
{
   border: 1px solid red !important;
}

client-side validation in custom validation attribute - asp.net mvc 4


By : user388476
Date : March 29 2020, 07:55 AM
I hope this helps . Take a look at this: http://thewayofcode.wordpress.com/tag/custom-unobtrusive-validation/
Using this tutorial I got my custom validation code running with no problem. The only difference I can spot in your code is the way you created the $.validator.unobtrusive.adapters.add function. The parameters are a little bit different but, maybe, the problem is just that you have not defined the rule part of your adapter.
code :
$.validator.unobtrusive.adapters.add("requiredif", ["requiredif"], function (options) {
    options.rules["requiredif"] = "#" + options.params.requiredif;
    options.messages["requiredif"] = options.message;
});
$.validator.unobtrusive.adapters.add("requiredif", function (options) {
    options.rules["requiredif"] = "#" + options.element.name.replace('.', '_'); // mvc html helpers
    options.messages["requiredif"] = options.message;
});

How To Do Client-Side Validation In Custom Validation Attribute In ASP.NET Core 2.0?


By : onclesam
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Your validation attributes have to implement IClientModelValidator and you have to write your own validation logic on client-side as well:
code :
public class UpperCaseAttribute: System.ComponentModel.DataAnnotations.ValidationAttribute, IClientModelValidator
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value != null)
        {
            string suppliedValue = (string)value;
            var hasUpperChar = new Regex(@"[A-Z]+");
            var match = hasUpperChar.IsMatch(suppliedValue);
            if (match == false)
            {
                return new ValidationResult("Input Should Have Uppercase ");
            }
        }
        return ValidationResult.Success;
    }

    public void AddValidation(ClientModelValidationContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        AttributeUtils.MergeAttribute(context.Attributes, "data-val", "true");
        AttributeUtils.MergeAttribute(context.Attributes, "data-val-uppercase", FormatErrorMessage(context.ModelMetadata.GetDisplayName()));
    }
}

public class AttributeUtils
{
    public static bool MergeAttribute(
        IDictionary<string, string> attributes,
        string key,
        string value)
    {
        if (attributes.ContainsKey(key))
        {
            return false;
        }
        attributes.Add(key, value);
        return true;
    }
}
$.validator.addMethod("uppercase",
    function (value, element, params) {
        return value === value.toUpperCase();
    });

$.validator.unobtrusive.adapters.add("uppercase",
    params,
    function (options) {
        options.rules[index] = options.params;
        options.messages[index] = options.message;
    });
Related Posts Related Posts :
  • 'Procedure or function 'GenerateTest' expects parameter '@questionnaireID', which was not supplied.'
  • Can ASP.NET target .net core?
  • What are effective options for embedding video in an ASP.NET web site?
  • Visual Studio "Unable to start debugging on the web server. The web server did not respond in a timely manner."
  • Tracking state using ASP.NET AJAX / ICallbackEventHandler
  • ASP.NET Display SVN Revision Number
  • ASP.NET URL Rewriting
  • Easy way to AJAX WebControls
  • How do I define custom web.config sections with potential child elements and attributes for the properties?
  • Are Multiple DataContext classes ever appropriate?
  • Triple Quotes? How do I delimit a databound Javascript string parameter in ASP.NET?
  • ASP.NET built in user profile vs. old style user class/tables
  • Bandwith throttling in IIS 6 by IP Address
  • Checklist for IIS 6/ASP.NET Windows Authentication?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org