I want to validate the ftp host name using knock out js. I have defined the validation in knockout like below:
selfLeadAdapter.Url = ko.observable(data.Url).extend({
required: { message: "Host required." }
}).extend({
pattern: {
message: 'Hey this doesnt match my pattern',
params: /((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)/i
}
});
But the regular expression is for validating the webpage or website. I want to validate the ftp host name. This will be in the form of ftp://ftp.example.com
you can use this expression :
/^ftp:\/\/(.*)$/i
you can be more specific, it's depend what you expect exactly.
By example :
/^ftp:\/\/(\w*\.\w*\.com)$/i
Related
How to extract the ID of the first object from the response? I am getting the below response of the API.
{"data":[{"id":1,"description":"Test description 1", "Location": "test location 1"}, {"id":2,"description":"Test description 2", "Location": "test location 2"}, {"id":3,"description":"Test description 3", "Location": "test location 3"}]}
I want to extract the id= 1, from the above response. I have tried with {"id":(.+?), regular expression. But I am getting randomly any of the id. What is the regular expression to get the first id from the response?
To get what you want (bad idea):
Regex:
"id":(\d+),.*
Get the result with \1.
Test here.
To get what you need: use a proper json parser. Regexes are not suitable for handling complex stuff (also including HTML among many others).
Use JSON Extractor instead of Regular expression extractor for the JSON response.
To extract the first id from the response you mentioned, use JSON path expressions as : .data[0].id
Example Screenshots:
Now, you can pass the variable test to your next API request as ${test}
I want to do client and server side validation for Textbox which should only accept URL. I am not able find any link which does the same.
Can anyone please provide some ideas?
#Html.TextBoxFor(m => URl })
There is already a UrlAttribute in the System.ComponentModel.DataAnnotations that you can apply to your property to give both client and server side validation.
[Url] // Add optional (ErrorMessage = "...") as required
public string Url { get; set; }
Internally, it uses the following regex (from source code)
^(https?|ftp)://(((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:)*#)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).?)(:\d*)?)(/((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|#)+(/(([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|#)))?)?(\?((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|#)|[\uE000-\uF8FF]|/|\?)*)?(#((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'()*+,;=]|:|#)|/|\?)*)?$
You can use a RegularExpression validation in your model:
[StringLength(200)]
[RegularExpression(#"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+#)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%#.\w_]*)#?(?:[\w]*))?)", ErrorMessage = "Not a valid website URL")]
public string MyUrlField { get; set; }
I have found the above regex for matching a url from here. You could also check this website for other regex which would match a url.
Note that you need to include jquery-validation-unobtrusive, for the client side validation.
You can do URL Validation using regex for validation.
^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$
OR
/^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$/
You can this regex in both Server-Side and Javascript.
check it on : JSFIDDLE
I have this url:
"url": "/application/userId=5678"
This url will contains the logged in userId, i am trying to map this userId with regex to be mapped on all url's in Wiremock Testing.
So far, i've tried this
"urlPattern": "/application/userId=[0-9]+"
But this is not working, any idea how to make this url work for any UserId?
RegEx should be something like:
^\/application\/userId=[0-9]+$
See also this question + answer:
Wiremock not matching regex
I am trying to "catch" a value from a json response using regex.
The response:
{"Result":0,"ResponseStatus":{"ErrorCode":null,"Message":null,"StackTrace":null,"Errors":null},"SessionId":"5a004b3b-f610-4a4e-9a0c-9953cee9bd47","ResponseHeader":{"Succeeded":true,"Errors":[]}}
I have created a HTTP request and added a child "JSON PATH EXTRACTOR" with the following data:
Destination Variable Name: SessionId_Regex
JsonPath Expression: "SessionId":"(.{8}-.{4}-.{4}-.{4}-.{12})"
The question is: how do i use the variable of the regex in a 2nd HTTP request
Thanks :)
JSON Path has nothing in common with Regular Expressions, you won't extract the value using above expression.
Change it to: $..SessionId[0]
You can refer extracted value directly as ${SessionId_Regex} or using __V() function like ${__V(SessionId_Regex)} where required
References:
JSONPath - XPath for JSON
Using the XPath Extractor in JMeter (scroll down to Parsing JSON)
I am using Jmeter 2.11 and My http response after login as follows,
"apiResponseStatus": "SUCCESS",
"authToken": "o5SsJF9yOebRC9LsR5WCnYUNpslddO30Db/zBAdhBW/ISfL62CaOHqmSkrAHZ8RT4XF6yaGxX6kbmpACZu212Q=="
How to write RegEx for this to reuse same token throughout the Test Plan?
Use Regular Expression Extractor with below details:
Apply to --> Main Sample Only
Response Field to check --> Body
Reference Name: auth_token
Regular Expression: .authToken": "(.+?)".
Template: $1$
Match No.: 1
Default Value: Not Found
You can pass Reference Name as ${auth_token} where it is required throughout your test plan.
Hope this will help.