handling special characters in parameters with XMLHttpRequest [duplicate] - javascript

This question already has answers here:
Pass a parameter containing '%' in URL?
(2 answers)
Closed 6 years ago.
Trying to send password using xmlHttpRequest from frontend(javascript) with POST and other parameters with names like
"&password=" document.getElementById('password').value
I'm using HttpServletRequest.getparameter to get the parameter string of password like string pswd = request.getparameter("password");
The code works fine with all passwords that have special characters except passwords like these qwe100%qwe, qwe198%qwe
When I pass those passwords, if I were to read username or other parameters sent along with password also results in exception[illegalArgumentException]
any help is appreciated..

You need to use encodeURIComponent on the JavaScript side before passing it to the back end.
That is:
var pass = encodeURIComponent(document.getElementById('password').value);
Note that on the Java side, you should then do a:
URLDecoder.decode(request.getParameter("password"));
EDIT
As #BalusC points out I'm wrong on the Java side - you have already done what is needed on the JavaScript side so you don't need the Java part.

Related

email regex not working properly with javascript/angular [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I have created a regular expression for email validations.
var regex = "^([a-zA-Z]+([\.-_]?[a-zA-z0-9]+)*)\#([a-zA-Z0-9]+)([-][0-9a-z]+)?\.([a-z-]{2,20})(\.[a-z]{2,3})?$"
To match emails:
1. update#update
2. mohit.bhagat#B-9com.com
3. mohit.Bhagat#us.thalesgroup.com
4. mohit#gmail.com.com.com
If you run this over online website 1 and 4th will fail while 2, 3 will pass.
But when I run this code in Javascript( Browser console ), 1st also passes the validation.
I am using Angular application.
The problem is how JS ignores \.
If you do following
var regex = "[a-z]+#[a-z]+\.[a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+.[a-z]{2,3}"
And if you do this
var regex ="[a-z]+#[a-z]+[\.][a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+[.][a-z]{2,3}"
Pay attention to [.] with this now i was able to get validation error for 1st email.
Complete regex: "^([a-zA-Z]+([-_.]?[a-zA-Z0-9])*)#([a-zA-Z0-9]+([-][a-z0-9A-Z]+)?)[.]([a-z]+[-_]?[a-z]+)([.][a-z]{2,3})?$"
Update:
Or you can do this: var regex ="[a-z]+#[a-z]+\\.[a-z]{2,3}"
As mentioned in comments below, you can do this way to consider . by including it in [.], but its a hack and not origional way to do it.
Best way to have . included in your regex is \.

JS - Transform single email validation pattern into multiple email validation pattern. [duplicate]

This question already has answers here:
Javascript multiple email regexp validation
(7 answers)
Validate a comma separated email list
(5 answers)
How to write regex to verify a comma delimited list of values
(4 answers)
Closed 4 years ago.
I am struggling to create a Regex pattern that would validate multiple emails. I know that this topic has been widely discussed, however, having researched them all I could not find the answer to my specific question. My issue is as follows.
The project I am working on is written in PHP and uses FILTER_VALIDATE_EMAIL. I aimed at writing a front-end email validator to use for both single and multiple emails in a way, consistent with FILTER_VALIDATE_EMAIL. Here https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js I have found the ideal Regex pattern compliant with latest RFC standard provisions, namely:
^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}#)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*#(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126})+(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))]))$
Now I've been trying to make this particular pattern function for multiple emails. Here comes my question, how can I get it done? I was trying to set
[\s*,]*
in order to make multiple addresses pass, by so far all I got is a headache, so your assistance is highly welcome, thanks in advance!
Try this..
var pattern = /^\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i
if(!pattern.test(inputYour))
{
console.log('not a valid e-mail address');
}​
for multiple email..
var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
validate(email.trim());
});
validate function should have your above code...
This is for check in one go...
var email = 'test#example.com, hello#example.com,mail#example.com';
alert ( (/^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#(([^<>()[\]\.,;:\s#\"]+\.)+[^<>()[\]\.,;:\s#\"]{2,})$/i.test(email)) );
link https://www.experts-exchange.com/questions/28940315/JavaScript-REGEX-validate-multiple-emails-addresses-separated-my-commas.html

How to Split string before '#' in angularJS [duplicate]

This question already has answers here:
How can I extract the user name from an email address using javascript?
(9 answers)
Closed 4 years ago.
I want to split an email of user before letter
#
in Javascript especialy in angularJS. for example if its
blabla#gla.com
it will turn into
blabla
can someone give me a simple example to make it?
because i must split it from API and store it as localstorage,
some of example that i find its use limitTo but can we use it to cut it in specific way from # until end?
Try this str.split()
var email = "blabla#gla.com";
var userName = email.split('#');
console.log(userName[0]);
Or use str.substring()
var userName = email.substring(0, email.indexOf('#'));
console.log(userName);

Complicated JavaScript string removal/replacement [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 7 years ago.
I have a query string that I need to remove a certain parameter from. For instance, my query string may be "?name=John&page=12&mfgid=320", and I need to remove the "page" parameter from it and end up with "?name=John&mfgid=320". I cannot assume that the "page" parameter is or isn't followed by other parameters.
All my attempts at using JavaScript functions/regex are failing miserably, so I could really use a hand in getting this working. Thanks.
That's quite easy... It's just /page=\d+&?/
var uri = '?name=John&page=12&mfgid=320';
uri = uri.replace(/page=\d+&?/,'');
You can use:
uri = uri.replace(/[?&]page=[^&\n]+$|([&?])page=[^&\n]+&/g, '$1');
RegEx Demo
We'll need to use alternation to cover all the cases of presence of query parameter. Check my demo for all test cases.

javascript url regexp restrict multiple http(s)/www [duplicate]

This question already has answers here:
Regular expression for URL
(10 answers)
Closed 8 years ago.
here is my regexp
var url_reg = /^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|in|au)+/;
it works fine for single input like https://www.google.com , but
it allows double or more "http/https/www" like below -
https://www.google.com/https://www.google.com/
url can also include folder like google.com/folder/file
i need to validate single occurrence of valid url.
Can anyone help me?
To validate a URL, you can use a regex. This is what I use. A valid URL per the URL spec. The URL you have provided, is actually a valid URL per the URL spec.
/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:#&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$/
This was borrowed from OSWAP

Categories

Resources