Sanjay Rathod
Sanjay Rathod

Reputation: 1143

How to display message with the help of jquery?

i m trying to do a validation using jquery. Enter Account no:

here is my html code

<input type="text" name="Assets" id="Assets" size="25" /><br />
<div id="eb"> Please Enter Valid Account no </div>
Enter Amount<input type="text" name="Liability" id="Liability" size="25" /><br />

and here is my jquery code

$(document).ready(function(){
    $("#Assets").change(function(){
        var Assets = $('input#Assets').val();
        var expression=/[0-9]{4}\s[0-9]{4}\s[0-9]{2}\s[0-9]{10}/;
        if (Assets == "" || Assets == " ") {
            $('#eb').show();
        }
        else if(Assets.test(expression)){
            $('#eb').hide();
        }
        $('#eb').toggle();
}

i want to display a mesage when user write a account no then at a time he can see the message that Please Enter Valid Account no and if he insert correct then that message can be hide.

here is my jsfiddle - http://jsfiddle.net/BzdfN/5/ please help

Upvotes: 0

Views: 107

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388436

jQuery was not included and multiple syntax errors

$(document).ready(function () {
    var expression = /[0-9]{4}\s[0-9]{4}\s[0-9]{2}\s[0-9]{10}/;
    $("#Assets").change(function () {
        $('#eb').toggle(!expression.test(this.value));
    })
})//missing brackets

Demo: Fiddle

Upvotes: 1

Sergio
Sergio

Reputation: 28845

Try this code:

$(document).ready(function () {
    $("#Assets").change(function () {
        var Assets = this.value;
        var expression = /[0-9]{4}\s[0-9]{4}\s[0-9]{2}\s[0-9]{10}/;
        if (Assets.match(expression)) {  
            $('#eb').hide();
        } else {
            $('#eb').show();
        }
    });
});

This is a suggestion for more simple code. Notice I used match() which takes a regex as paramenter, if you want to use test() you should use the value/string as paramenter and use like expression.test(Assets).

Fiddle

About your code/fiddle:

  • you missed adding jQuery library to the fiddle
  • you missed ) closing the change() function
  • you missed }) closing the ready() function
  • you used test() with wrong order, check my text above about match() and test()
  • your fiddle with corrections: Fiddle

Upvotes: 2

Dan Goodspeed
Dan Goodspeed

Reputation: 3590

Try this - http://jsfiddle.net/dangoodspeed/BzdfN/9/ I made a bunch of changes, including activating jQuery, closing some functions, and reversed Assets and expression for the .test call

        else if(expression.test(Assets)){

Upvotes: 0

Related Questions