0

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

1
  • 1
    By default error message should be displayed and if the account number is valid then it should be hidden...Is it so?? Commented Oct 19, 2013 at 7:19

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

there isn't any message in your fiddle?
@nrsharma, I made a new version.
if i am insert 56f in first textbox then it should display error message without enter into second text box.is it possible. i know this is silly question but i am very new to jquery
@SanjayRathod, it already does, but only when you click away and apply the change. Otherwise you should use keyup event like this: jsfiddle.net/evAr7/1
validation completed in my fiddle buut still there is one problem. i want that if user not inserting any value and go for a next textbox by pressing tab then he or she can be see error message.how to do this
|
1

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

1 Comment

if i am insert 56f in first textbox then it should display error message without enter into second text box.is it possible. i know this is silly question but i am very new to jquery
0

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)){

4 Comments

if i am insert 56f in first textbox then it should display error message without enter into second text box.is it possible. i know this is silly question but i am very new to jquery
If I understand the question right, you want it updated every time a key is pressed instead of when leaving the text box? Just update .change to .keyup. Like here - jsfiddle.net/dangoodspeed/BzdfN/24
I have implemented jsfiddle.net/F6c5U/1 but this is not working when i am implementing in localhost.
So it works on JSfiddle but not on your own computer you say?

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.