views:

50

answers:

3

Hello friends..

I have a text box I need to validate that..

I mean user can only Enter Either A or B or C if he enters D to Z or any other things I should show the Popup message please Enter A or B or C

Using Jquery or javascript?

thanks

+1  A: 

user regular expression.

<script>
 function validat(str)
 {

     if(/^[A-C]*$/i.test(str)==true)
    {  
         //allow your code
    }
    else
    {
       alert("please enter only abc");
    }
  }
</script>
Akie
i is for ignore case. if want to allow only caps then remove i. also you can call this function on onclick event of button. like <a type="button" onClick="javascript:validate(this.text);"/>"
Akie
+4  A: 

Do you really need to allow user input via a text box?

I would suggest making use of a set of radio buttons instead of a text box in that situation.

<form>
<label for="A">A</label><input type="radio" name="A" value="A" />
<label for="B">B</label><input type="radio" name="B" value="B" />
<label for="C">C</label><input type="radio" name="C" value="C" />
</form>
tjk
+1 to this, even though it doesn't really answer the question ;-). Alternatively, as I mentioned in the comment above to the OP, a select would work just fine.
treeface
@treeface Sorry about that :-) I'm new to this- I noticed you'd beaten me to it just after I'd posted it - should an answer (which isn't technically an answer) be posted as a comment instead?
tjk
@tjk No no. I didn't mean to say that what you posted was bad. I think the "answers" on SO are more "responses" to questions, anyway. An answer that starts a discussion is great because it usually leads to the right answer later in the thread. Someone else will come along with the direct answer, unless it's just far too much in the wrong direction. With this question, it's difficult to tell from lack of context, so your response was perfectly valid.
treeface
+1  A: 

See this Live

I hope you really want like this

vinothkumar