Multiple Regex Validation against a value - regex

I want to validate a text box input with below requirement
Text must be integer
Value of text must be between a predefined Max Value and predefined Min Value
** I don't want to use RangeAttribute.I want to use regular expression which I want to store in database and will get applicable when View will get rendered.
How we can do this in c# using multiple regex?

IF you want to restrict it in HTML, so you are sure the value you will receive is already a number you can do it like this:
Quantity (between 1 and 5): <input type="number" name="quantity" min="1" max="5">
IF this is an MVC project, you should restrict this in your ViewModel by setting your property to be an Integer (int), use annotations to enforce things like
[Required]
or
[Range(10, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
Depending on what your project is built in (Webpages or MVC) this can be done differently.

Related

Django: display a preview of an object's attribute - Class based views

Using a ListView Class-based-view, I am looping over the objects present in the database of a certain model in my HTML template, and, for instance, I can access an object's "body_text" attribute with the following syntax: {{object.body_text}}
What if I wanted to only show the first 20 characters of that "body_text" attribute in my HTML template?
How can I set that?
1st Method
Use the truncatechars filter in your HTML template.Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis character (“…”).
{{object.body_text|truncatechars:20}}
Reference:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#truncatechars
2nd Method
Use the slice filter in your HTML template.
{{object.body_text|slice:":20"}}
Referernce: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice
Found it, eventually.
Use the |slice:":" filter in your HTML template.
For example, if you only want to display the first 10 characters of a given attribute, use:
{{object.body_text|slice:":10"}}

Regex expression with Validators.pattern for just integer number

I am trying to create a validator for only integers (e.g., 60) using Regex.
Here how I am trying to do it:
meter: ["", Validators.required, Validators.pattern(/[0-9]/)]
It does not work, but I have to say I am not familiar with this technique.
Anyone could shed some light on how I could modify this expression to accept just integers?
This number is for height, and the height is devided into meters and centimeter.
I have searched, but I could find for decimals and so, somehow, maybe because it is too trivial, for just integers, I cannot find, in fact, I have found yesterday here, but I cannot find the answer anymore I have found, I was looking for another thing.
First of all, you don't need a custom validator to do this, as regular expression validation is already a built-in validator.
Secondly, the way you apply validators to your form controls depending on either you are using template-driven forms or reactive forms.
If you are using template-driven forms (which is commonly used for Angular beginners), simply make your input control template look like this:
<input type="number" name="meter" required pattern="[0-9]" [(ngModel)]="meter">
If you are using reactive forms, just assign both "required" and "pattern" validator to your form control.
<input type="number" [formControl]="myControl">
myControl = new FormControl(null, [
Validators.required,
Validators.pattern("[0-9]+")
]);
When using it in a reactive form control, you can pass in either a string or a regex object, there are differences between them as stated in official docs:
If a string is passed, the ^ character is prepended and the $ character is appended to the provided string (if not already present), and the resulting regular expression is used to test the values.
Lastly, a Stackblitz example attached to show simple usage of this validator in both template/reactive forms.
https://stackblitz.com/edit/angular-ivy-neux3n

TFS 2013 backslash in custom field

I am trying to add a custom field in a work item template using TFS power tools. I add the new field and use the "ALLOWEDVALUES" rule to add some values. It allows me to put backslash character in the value field however when I try to save the work item, it throws an error: VS402504: User or group cannot be found: Category\SubCategory. Verify that the users and groups used in your work item type definition exist.
I could find a way to escape the backslash character. Is it not possible to put backslash characters in custom fields?
The backslash is supported in the filed value.
ALLOWEDVALUES Defines a list of allowed values for the field. Allowed values are values that are available for selection in a field
list on work item forms and in the query builder. You must select from
one of these values.
Note: When you use ALLOWEDVALUES to define a list of values that users can specify in a work item form or the query editor. Users must specify one of the values in the GLOBALLIST or the set of LISTITEM entries.
A sample of value for your reference:
<LISTITEM value="Emergency"/>
<LISTITEM value="Major"/>
<LISTITEM value="Minor"/>
<LISTITEM value="Domain\joe"/>
<LISTITEM value="[Global]\GlobalGroup" />
<LISTITEM value="[Project]\ProjectGroup" />
More details about related info please take a look at official tutorial: Define pick lists
For field pattern value: ^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)+$, also check the field definition here.
Update:
If the field value contain backslash(\) characters, it will be parsed to an IdentityName(account) by default. As a workaround, you could try to use / instead. Take a look at this similar question: Is there any way to use a backslash in a TFS Global List value?

regular expressionn not able to correlate my test scenario with viewsate in jmeter

I have tried with regular expression also with css/j query extractor,not able to run my script properly.
My exact test scenario is kind of
There is kind of application form.
First Load the page.
Fill with some data, then go to next page.
Now again fill some details, and Submit.
Note:For Each and every 'application form' there is two "9" digit unique number , which gets generated from server side, and after participating the form there is two options:
a)Access Count
b)Response Count
Every time J meter thread group hits the page 'Access count' properly gets increased , but Response Count not gets change, it should get increase for each user as well.
I have tries to correlate both the parameter using regular expression and css/j query expression , but still the 'Response Count' not gets increment. What should I do for this case to do Load test in J meter.
Viewstate regular expression
Change parameter
Regular Expression: input[id=_VIEWSTATE]
While Checking the response data in
result tree its showing different in Load page and next Page.
Add Regular Expression Extractor as a child of the first request.
Configure it as follows:
Reference Name: VIEWSTATE
Regular Expression: <input type="hidden" name="javax\.faces\.ViewState" id="javax\.faces\.ViewState" value="([^"]+)".*/>
Template: $1$
Refer extracted value as ${VIEWSTATE} where required.
By the way, you can test your Regular Expressions right in View Results Tree listener using RegExp Tester view.
The regular expression is wrong.
input[id=_VIEWSTATE] can' t capture a value.
You can test it in regular expression tester of response.
It may be something like: input[id=(.*?)]
It depends on your dynamic value. But in question you didnt mention screenshot of that dyanamic data.

Custom field / form field (maybe widget?) for storing values for a string format

I wish to create a model where I can store a string formatting pattern as well as accompanying values.
Example:
Pattern = 'Strength deals %d more damage for each %f%% critical chance'
Values = [2, 1.50]
The pattern only has to store ints and floats. My initial thought was to simply create a custom field, validate the values and that would be it. You can see my field here
However, this simply lets me save the list. I still need some way to figure out how many values to validate, check that it matches the other field (a simple CharField). This could be done "manually", but I would like to create a custom form field that would generate X input boxes that match the number of wildcards in the pattern.
Question 1: Is there any way to "link" two fields so that one can act on the value of the other?
Question 2: To create this, I would probably need to create a new form widget, but is this possible? Any hints as to how to start?
This may not exact answer, but writing it in comment is not feasible.
Another option would to store as JSON string in the model. Like
[{ "Pattern": 'Strength deals %d more damage for each %f%% critical chance',
"Values" : [2, 1.50]
}]
Use custom form field to input/output as JSON input. There are quite a few implementation available when searched on google.
With this approach, you can try to validate the inputs, so that format string and number of variables provided matches.