tags:

views:

85

answers:

5

How do i write a regular expression which accepts only value "1" in the textbox and it should not accept zero or greter than "1"

+2  A: 
/^1$/

… but for a "Must be this value, exactly this value, and nothing but this value" test, you would be much better off with a simple string comparison.

David Dorward
@david: How do i add This to the existing one ^[0-9\ ]+$ to this and form the expression /^1$/
Someone
@David: There is one problem But i cannot explain and it is solved now
Someone
+8  A: 
if (theTextBoxValue == "1") {
   // accept
} else {
  // reject
}

You don't need regex for this simple task. And, if you only accept "1" in a user input, why provide such an input to the user at all?

KennyTM
+1  A: 

/^1$/ but a simple == "1" will be enough in most languages (or .equals("1")).

Colin Hebert
A: 

Don't think you want to use regex for this.

Pattern: ^1$

Fabian
+1  A: 

Hi, I saw your previous question (using the jquery validationEngine), and I was intrigued by it so I started looking.

The problem with using a funcCall (as you were trying in the old post) is that the function is called without any context (has no arguments, and has this == window), so you can't tell which input field is being validated.(I solved this with a trick - see example & comments linked below).

Another solution I found was using regex(as you are trying now). This is the entry you have to add to the languages file:

"isOne":{
        "regex":"/^1$/",
        "alertText":"* Only '1' is valid [regex]"
    }

This is how you use it on the input field:

<input type="text" class="validate[custom[isOne]]">

And this is how you start the validation engine:

$('#form').validationEngine({
    validationEventTriggers:"change"
});

You can view a working example using both function and regex here

Dan Manastireanu
@Dan: At Least you understand my problem So i opted for Regex and was able to solve it easliy
Someone