soundy
soundy

Reputation: 307

How to validate TextBox and label values

Need to validate label values(get from database) and TextBox values(user enters). If Textbox field value is greater than label value means, have to show error or warning message.

enter image description here

Upvotes: 0

Views: 2009

Answers (2)

ThinkingStiff
ThinkingStiff

Reputation: 65351

Here's a simple Javascript validator that does what your looking for.

Demo: jsFiddle

Output:

output

Script:

document.getElementById( 'numbers' ).addEventListener( 'keyup', function ( event ) {
    if( event.srcElement.className == 'validate' ) {
        var value = event.srcElement.value,
            validationValue = event.srcElement.nextSibling.textContent;
        if( isNaN( value ) ) {
            event.srcElement.nextSibling.className = 'error-nan';
        } else if( parseInt( value ) > parseInt( validationValue ) ) {
            event.srcElement.nextSibling.className = 'error-too-large';
        } else {
            event.srcElement.nextSibling.className = '';
        };
    };
} );

HTML:

<form id="numbers">
    <input class="validate" /><label>15</label><br />
    <input class="validate" /><label>7</label><br />
</form>

CSS:

.error-too-large::after {
    color: red;
    content: 'value too large';
    padding-left: 5px;
}
.error-nan::after {
    color: red;
    content: 'value not a number';
    padding-left: 5px;
}

Upvotes: 1

Alexan
Alexan

Reputation: 8637

You can use ASP.NET Validation Controls. They use client and server validation.

Upvotes: 0

Related Questions