0

I Need a custom form validation in a textbox

The value should be like this 3-5, so <number>-<number>

Please suggest me some ideas

2
  • did you manage to find a solution to your question? Commented Nov 10, 2015 at 14:19
  • no solution found this Commented Nov 11, 2015 at 4:55

1 Answer 1

1

You can create custom Javascript validation for any form. Inchoo has a post about that and Alan Storm also wrote something about it

After the form is initiated in your html, which would look something like var theForm = new VarienForm('theForm', true); add the following

Validation.add('validate-must-be-number-dash-number','Is not number-number!',function(the_field_value){
    if(the_field_value.match('/[0-9]-[0-9]/ig'))
    {
        return true;
    }
    return false;
});

So, in full; it would look something like this in your form phtml file

<form ... id="theForm">
   [...]
   <input type="text" class="input-text validate-must-be-number-dash-number" value=""/>
   [...]
</form>

<script type="text/javascript">
var theForm = new VarienForm('theForm', true);
Validation.add('validate-must-be-number-dash-number','Is not number-number!',function(the_field_value){
        if(the_field_value.match('/[0-9]-[0-9]/ig'))
        {
            return true;
        }
        return false;
    });
</script>

Now add the class validate-must-be-number-dash-number to your input and you're done

9
  • 3-5 example i need like that Commented Oct 28, 2015 at 10:20
  • I don't think I understand you, what is not ok about above example? Commented Oct 28, 2015 at 10:21
  • in this we can use 3-5 only right Commented Oct 28, 2015 at 10:26
  • That is correct, this checks if the value is 3-5, you can alsways tweak it to check for another value. It's basic javascript, nothing fancy there Commented Oct 28, 2015 at 10:27
  • that's example i need that format not only 3-5 Commented Oct 28, 2015 at 10:29

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.