Gansun
Gansun

Reputation: 301

How validate a text box using jquery?

I have text box (telephone number) which is coming under the div tag not coming under the form tag. I have to validate this text box using RegEx pattern. If the the pattern is not matched I should not allow the users to save the screen.

I thought of using jquery validator

$.validator.addMethod

$('#form').validate

But the problem is this text box doesn't comes under form tag. What are the other options I can choose?

Upvotes: 1

Views: 595

Answers (1)

SpYk3HH
SpYk3HH

Reputation: 22570

It would really help to see some code.

Without code I can tell you this, there are SEVERAL ways to achieve what you want very easy.

If you're textbox (and i'm just assuming here) is a noraml input or textbox, you could do validation on either the keypress (checking if a number is being entered, and then checking the input value as a whole, return false if there is an error and no character will be inserted) or you could check them on 'blur', this would give you a function to check validation when they click anything else other than that textbox.

Or, in jQuery, you can use a form ajax submit and use the function 'beforeSend' to validate form before posting and return false if there is an error.

Gimmie a lil example detail code and i'll show you more...

also, is there any reason you can't include the textbox in the form?

Upvotes: 2

Related Questions