2

I am having an aspx form and in that i need to do validations using jquery or javascript.

I just want to give a message near to the textbox if a user enter a value which is not valid in that textbox.Inorder to display the message in a popup [not alert('message')]

How can I find the position of the textbox in which user enters the invalid data or how can i display a message near the textbox using javascript or jquery ?

I need the validation occur in blur.So it is easy for user to know whether he entered a valid data immediately after giving the input.

Thanks in advance.I am not interested to use asp.net ajax validation and its callout extender.
I just want to implement a functionality which is similar to validation callout extender does.

4 Answers 4

1

When you bind the blur event to the textbox, you know which textbox it is. Just use your callback javascript to insert the error near by.

If you're doing jquery, it might look something like:

$('.my_textboxes').blur(function() {

var textbox = $(this);

//ajax validation call
$.post('/whereever', {}, function(response) {
    //ajax callback
    if (response == 'error') {
        textbox.next().html(error);   //something like this to insert the error into the next element after the texrbox, eg, a span to hold the error
    }
});`
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the jQuery (ASP.NET) Validator Callout Plugin

Comments

1

You can use the jQuery validation plugin

Which has options to specify the message beside the control or to specify all the message at one place.

The demos are available at http://docs.jquery.com/Plugins/validation

1 Comment

I think u misplaced the url of jquery validation plugin.
0

Exactly how you do this depends on the layout of your form, but in general you probably would do something like this: if the validation returns an error for a text field, check to see if an "error box" already exists for that field. If so, then just update its contents with the new error message; if not, then add the error element.

One way to do that would be to use a <span> tag with a particular class (for layout purposes), and an "id" value made from the input field's "name" or "id":

$.fn.setValidationResult = function(errorMessage) {
  return this.each(function() {
    var errId = 'errMsg_' + this.name, errMsg = $('#' + errId);
    if (!errMsg.length) {
      $(this).after($('<span></span>', { class: 'validation-error', id: errId }));
      errMsg = $('#' + errId);
    }
    if (errorMessage)
      errMsg.html(errorMessage).show();
    else
      errMsg.html('').hide();
  });
});

Then you can just use $(yourInput).setValidationResult(whatever); with "whatever" being empty when validation passes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.