One asp.net validator for several TextBoxes - javascript

I have 3 TextBoxes on the page. How do I setup one validator (and what's kind of validator?) to validate each of TextBoxes on client side?
First textBox must contains only 1-3 number of digits, second - infinite number of digits, third - 0-5 number of digits

It would be better to use different validators for different text boxes.
Use 3 validators for each with display message as * sign.
Then use Validation Summary control to print a single message for all of them, as you want a single message to be displayed for all. Refer this link: MSDN Validation Summary
Check this too: Validation Summary to work with client-side validations

Your expressions would be along the lines of:
[0-9]{1,3}
[0-9]+
[0-9]{0,5}
Updated following further info:
You could combine the values from each text box into a comma separated value and then validate that using [0-9]{0,3},[0-9]+,[0-9]{1,5} expression.
Either use javascript to copy the value into a hidden textbox field with it's own regex validator using above expression, or just perform the validation manually.
e.g. Using jquery
$('#hiddenCombinedField').val($('#field1').val()+','+$('#field2').val()+','+$('#field3').val());
Run this just before you call validate on the form.
See also http://speckyboy.com/2009/12/17/10-useful-jquery-form-validation-techniques-and-tutorials-2/ for some useful reference material.

You could use a CustomValidator but i would prefer using three distinct validators, one for every TextBox.
It's possible to leave the ControlToValidate-property of the CustomValidator empty. On this way you can validate multiple controls at the same time.

Related

Matching varying length in JS Regex

Let me explain my query with an example:
Am capturing page name from a web site. Due to design, the page name can be of varying length:
It can be
Data1|Data2|Data3
Data1|Data2|Data3|Data4
Data1|Data2
I need to write a Regex which comes true on all the above scenarios. I have something below shared by a previous user:
/(.*?)\|(.*?)\|(.*?)\|(.*)/gm;
The above works well when the string is always of four group, and there is a blank in between. But if I just have two values the regex fails. Can any user please guide?
Not sure what you meant there but does this help? But it will only accept alphanumeric values and a space
/([a-zA-Z 0-9]{1,}\|){1,}[a-zA-Z 0-9]{1,}/g
This will expect at less two Data field, and at most 4 fields
/(?:([^|]*)\|){1,3}([^|]*)/gm;
If you also want only one field (no pipe):
/(?:([^|]*)\|){,3}([^|]*)/gm;
{n,m} means allowed to repeat n trhough m times
Notice how I used [^|]* instead of .*?, so I match anything but the pipe |, also I used non matching groups (?:) so the groups that includes the pipes are invisible, i.e. you can get the fields as get them before

Javascript mask string

I am creating a ASP.NET web app and I need to mask the input string in a textbox. The string needs to be masked like:
DR117-17
So, two characters(DR), followed by 3 digits, followed by a '-' and followed by the fiscal year(yy).
The user has to actually input only the 3 digits (ex:'117').
The perfect solution would be that the characters 'DR' are automatically inserted on click, then wait for three digits to be input and then automatically add the '-17'.
Using the jQuery mask plugin is pretty straightforward:
$("#date").mask("DR999-17");
Working fiddle: http://jsfiddle.net/55003z11/

Angular.js ng-pattern strange behaviour

I have been working on a form that accepts Twitter parameters such as # and # to populate a Twitter feed.
With Angular.js I had planned to use the built in ng-pattern directive to validate the input before saving, however the validation is acting extremely strangely. It marks a "valid" string as invalid on every 2nd character of the input while typing.
Its quite hard to explain the exact behaviour so heres a Plunker.
For completeness I will add my input field with the strange ng-pattern here:
<input type="text" ng-pattern="/(^|\s)#(\w+)|(^|\s)#(\w+)/g" ng-model="foo" name="foo"/>
It's because of the global matching with the g option, it works if you take it out.
Calling test or exec multiple times involves state:
As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match.
Basically it's trying to move on to another match, but can't find one:
a = /#(\w+)$/g;
> /#(\w+)$/g
a.exec("#test")
> ["#test", "test"]
a.exec("#test")
> null

validation for the "version" field in a table

I have an input field named "VERSION" in a form where I have to write the version of the document.
e.g.
Version : 10.1.2.3
How to validation for this input field in javascript?
Can you use a regular expression?
(\d+\.)+.\d+
Meaning: one or more digits, followed by a point, repeated as many times as you want, followed by a point and one or more digits.
If you have to have 4, you can make it more specific:
(\d+\.){3}.\d+
If it has to be 2 digits, one one one, try:
\d\d\.\d\.\d\.\d
As you see, you need to be more clear about "what is a valid version, and what is not" in order to obtain a more specific answer :]
You mean you'd validate if there is a version input in the correct format?
(use an ID for the input field)
if(document.getElementByID('VERSION').value.match(/^\d+\.\d+\.\d+\.\d+$/)) {}
This should match all versions in the format {number}.{number}.{number}.{number}
Maybe you have to trim the string before you do that

Birthday input field - auto-adding dots after specific amount of characters

Hey guys,
unusual question: I have a input field #birthday that should accept european date format like this. dd.MM.yyyy
I wonder if it is possible to auto apply dots when typed into this field.
Imagine the inputfield is focused and empty. I start typing to numbers and the field autogenerates a dot, again two numbers and a dot is generated.
My aim is to type only numbers without needing to add the dots.
any idea how i could achieve that with jquery or javascript in general?
$('#birhtday').keypress(function() {
// ?
})
Why not use existing jQuery plugins?
Masked input
meioMask - I find it nicer because it doesn't use those underscores (but it seems to have some bugs when TABing through filled form)

Categories