1

i have small function that running on condintion, if text box value is not equal to "jitender"then alert will come up, but i also want reset the text box value if text box value is not equal to "jitender".

$(function(){
    $('button').click(function(){
        var name= $('#name').val();
        if(name!='jitender'){
            alert("not valid name")

            }

        });

    });


<input type="text"  id="name"/>

<button>click me</button>

5 Answers 5

1

you can simply add

  if(name!='jitender'){
            alert("not valid name");
            $('#name').val(''); //here comes clearing input
            }
Sign up to request clarification or add additional context in comments.

Comments

1
$(function(){
    $('button').click(function(){
        var name= $('#name').val();
        if(name!='jitender'){
            alert("not valid name")
                $('#name').val(""); // this will reset the value
            }

        });

    });


<input type="text"  id="name"/>

<button>click me</button>

2 Comments

That will blank out the box. Seems to me "reset" means "set back to what it was when the form was loaded". May be the same thing in this case...may not.
The code do not modify the text-box. so "reset" means "make it empty" I guessed :)
1

Try this.

$('button').click(function(){
        var name = $("#name");
        if(name.val() != 'jitender'){
            alert("not valid name");
            name.val('');
        }
    });
});

Comments

1
$(function(){
    $('button').click(function(){
        if($('#name').val() !='jitender'){
                alert("not valid name")
                $('#name').val(""); // resets value
            }

        });

    });​

<input type="text"  id="name"/>
<button>click me</button>​

heres the link so you can test: Test the script

Comments

0

You can simply call this after your alert - $('#name').val('newValue');

See here - http://api.jquery.com/val/

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.