I have the following regex for URL that require to start from http or https
(http:|https:)+[^\s]+[\w] that work very good for what I want (https://regexr.com/598hs)
But when I try to run it in angular validation, it doesn't work.
this.form = fb.group({
numberCode: ['', Validators.pattern(`(http:|https:)+[^s]+[w]/g`)]
})
https://stackblitz.com/edit/angular-reactive-forms-validation-lulnas
Why so ?
You can also try matching by this:
Validators.pattern(/^(http:|https:)+[^\s]+.+\.+.+$/)
Related
I want to validate if an email is being send correctly in routes, however it isn't working when I put a regex condition (like explained here)
Basically, I added:
Route::group([
'namespace' => 'Auth',
'middleware' => 'api',
'prefix' => 'auth'
], function () {
Route::post('login', 'AuthController#login');
// ...
Route::get('checkemail/{email}', 'AuthController#check_email')->where('email', '#.*?\.');
});
even if I pass a valid email I get an 404 not found. what can I do to make the code works well?
It looks like you're missing .+ before # in your regex.
Edit: I forgot to mention, you can put the same thing after #, and the current regex seems like it's wrong as well. I won't go into it, but validating email should not be complicated for a lot of reasons.
I have this custom Regex statement to validate websites on my app. I have everything working correctly except one case.
If I use query params in my URL, it doesn't pass the regex.
Here is what I currently have:
pattern="^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*$"
Current Output: Fails on URLs such as http://google.com/hello?world=foo
Expected Output: Allow query params in the URL.
I am designing a community site that will allow users to login and befriend others, and their account will be accessible via the following routes:
https://domain.com/en/community/id/:userid AND
https://domain.com/en/community/users/:username
However I want to always want users to use the first URL as it looks more aesthetically pleasing. I could just redirect straight from the ID route, but with the planned friends and groups, I will end up with URLS such as:
https://domain.com/en/community/id/:userid/friends
https://domain.com/en/community/users/:username/friends
https://domain.com/en/community/id/:userid/groups
https://domain.com/en/community/users/:username/groups
How can I always redirect from /id/:userid to /users/:username but keep the rest of the URL?
The first task is matching all URLs with /id/:userid to use the same route callback.
The second task is to replace the /id/:userid with /users/:username.
Can these two tasks be achieved with Regular Expressions?
Thank you very much.
Okay, it turns out it was very simple to allow the user ID alias.
I can use an asterisk to capture all the other URLs, and then use a simple RegExp replace call to replace the ID with the Username.
app.get('/:lang/community/id/:id/*', function(req, res){
var id = req.params.id;
User.getByID(id, function(err, result, user){
var url = req.url.replace(new RegExp('/id/'+id, 'g'), '/users/'+user.name);
res.redirect(url);
}
});
This is the best solution I could think of. I hope it helps other people.
I have absolutely no idea about regex at all. I am trying to perform email validation in a PHP signup script (to check if a valid email has been entered). I have got a script from the internet but I don't know how it works and I am completely unfamiliar with the functions used. I found out that the function they used (eregi_replace()) is deprecated in favor of preg_replace(). Firstly, can someone guide me through the steps of the function below, and secondly can you explain the preg_replace() function and how it works?
The validation script:
$regex = "([a-z0-9_.-]+)". # name
"#". # at
"([a-z0-9.-]+){2,255}". # domain & possibly subdomains
".". # period
"([a-z]+){2,10}"; # domain extension
$eregi = eregi_replace($regex, '', $email);
$valid_email = empty($eregi) ? true : false;
The script was sourced from here
regex are not needed here:
filter_var('bob#example.com', FILTER_VALIDATE_EMAIL);
if you want training i suggest you to visit http://www.regular-expressions.info and http://php.net
To quickly test a pattern you can use this online tool: http://regex.larsolavtorvik.com/
I have a url in which I would like to make the status token optional. If the status token is not provided in the url I give a default value in the view method argument. I tried replacing the token with this (?:/(?P<status>\d+))?$ but doesn't seems to work well. Thanks
url(r'^(?P<status>\d+)/$', frequest_list, name="frequest_list"),
def request_list(request, status=1):
...
...
Update:
This was the pattern I was trying:
url(r'^(?:/(?P<status>\d+))?$', frequest_list, name="frequest_list"),
So, if I try localhost/features/ works well
But if I do localhost/features/1/ it fails
Just create a second url entry that calls the same view:
url(r'^features/$', frequest_list, name="frequest_list_default"),
url(r'^features/(?P<status>\d+)/$', frequest_list, name="frequest_list"),
I use single url optional captures in some of my projects, and they work fine. You might want to adjust your pattern to make the trailing / optional. I think that is what is causing your url to not match. Django does have an "APPEND_SLASH" settings bool that will add that on to your urls if they are missing it and don't match:
url(r'^features(?:/(?P<status>\d+))?/?$', frequest_list, name="frequest_list")
The optional / could probably also be written like this:
url(r'^features/?(?:(?P<status>\d+)/?)?$', frequest_list, name="frequest_list")