Pregunta

Can you please tell me how to do validation in a form which is generated dynamically? I am using one plugin dform.js which converts JSON to form. I am to do that validate of fields.

http://jsfiddle.net/Xe3FG/2/

I take help from this.

https://github.com/daffl/jquery.dform

In my demo, I take 2 number fields. If the user enters a string and go to next field, I need it to display an error in front of the field, "please enter only numbers." I need the same with second field.

Can we do only using drom.js or validation.js?

I am able to validate when user enters data in the field and then press enter.

So I used blur event. It is not a good practice to use blur event on every field. Can you give a different way and a good way to validate?

 ("#totalRetryCount").blur(function() {
    // Number element type returns empty value when NaN
    if ( $('#totalRetryCount').val() == '' )
        alert('enter a number');
});

$("#totalRepeatCount").blur(function(event) {
    // Number element type returns empty value when NaN
    if ( $('#totalRepeatCount').val() == '' )
        alert('enter a number');

I used these two blur events. I don't want to use these events. Can we do these validations another way?

¿Fue útil?

Solución

I've already explained to you how to do this in your last question and provided you with a fully working example which you've seemed to completely ignore. In this case, you would just need to check if the value is an empty string, as that is the default value of a number type input field containing non-numeric data.

Fiddle demo

$("#testSuiteConfigurationform").validate(validateInputParameters());

function validateInputParameters() {
    jQuery.validator.addMethod("onlyNumbers", function(value, element) {
        return value != "";
    }, " Please enter only numbers");

    var validation = {
        onfocusout : function(element) {
           $(element).valid();
        },
        rules : { 
            totalRetryCount: { onlyNumbers: true }
        },
    };
    return validation;
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top