In my application users enters mobile number's country code with '+' symbol. Right now I am checking for + symbol. If they enter '+INDIA', according to my code this is correct. But according to logic this is wrong. They needs to enter '+91'. Means they can enter only 1, 2 or 3 digit number with '+' symbol. for example '+1', '+91' or '+234'. How I can achieve this type of validation in android?
You could write your own Pattern to check the format. I could give you the example, but I think the site I linked explains it much better.
It should probably start with something like \+\d\d?\d?
You could try using Google's own implementation of Phone Numbers or this xml list.
Hope it helps !
Related
In an android project, im trying to validate a password that the user inputs, and it must follow some rules
The rules are:
it must have 7 characters and 3 of the following conditions
**
-One lowercase character
-One uppercase character
-One number
-One special character
**
for example:
asd123!!!
PPPppp000
TTT999###
i was trying with this regex
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{7,}+$
but this enforces all rules at same time.
The approach is wrong here. The regex you created looks like a monster from under the bed, and is highly illegible even for someone regex-literate.
Why not split it into 4 (or as much as there are rules) regexes and check against whether 3 of them return a match? Not only will you make your regexes cleaner, but you will be able to add more rules if need be without changing whole regex.
You can also use inbuilt methods for checking (if applicable under Android development kit).
Some pseudocode would look like this:
result1 = Regex.IsMatch(password, rule1regex)
result2 = Regex.IsMatch(password, rule2regex)
...
resultN = Regex.IsMatch(password, rule3regex)
if(three_out_of_four_rules_apply)
password_valid = true
You can also apply method suggested in comments by #pskink and iterate over each character of a password and set the output accordingly.
Without going into the details of your lookaheads (which seem correct), here's how you would need to implement "three out of four criteria" in pure regex :
(?=.*A)(?=.*B)(?=.*C)|(?=.*A)(?=.*B)(?=.*D)|(?=.*A)(?=.*C)(?=.*D)|(?=.*B)(?=.*C)(?=.*D)
You can test it here.
Factorizing doesn't really make it better :
(?=.*A)(?:(?=.*B)(?=.*(?:C|D))|(?=.*C)(?=.*D))|(?=.*B)(?=.*C)(?=.*D)
I obviously recommend using a higher level language to implement these sorts of constraints.
i want to create an android application that can search word in sqlite database using approximate string matching.
for example if someone misspelled the word "switch" with " swithc", the sistem will correct the word and show message "did you mean 'switch' ".
its like google that can correct wrong word. how can i do it ?
have a look at this answer
Java: how to find the most probable string in a list of strings?
you can use the way of string matching as you desire. there is also a github project for this at:
https://github.com/northpoint/blur
I'm trying to learn android, I'm having trouble with user input
I've used editText to get users to enter a date and email which is all fine, then i add a hint
for these fields like so:
android:hint="#+string/DD/MM/YY"
android:hint="#+string/example#example.co.uk"
and i get multiple marker errors in the generated files :/
I don't think you can use special characters in string names. Give it a meaningful name like support_email. You can find more information here.
Pattern pattern = Pattern.compile("[a-zA-Z]+&");
myCustomLink.setText("press Linkify& or on Android& to search it on google");
Linkify.addLinks(myCustomLink,pattern, "http://www.google.ie/search?q=");
This code works perfectly but I cannot get it how patterns works and convert only Linkfy and Android as a link ???
It's a regular expression.
http://www.marksanborn.net/howto/learning-regular-expressions-for-beginners-the-basics/
http://www.regular-expressions.info/reference.html
it's saying get ' Letters followed by the &(ampersand) sign ' if you changed it to a . (fullstop) the . character has a special meaning in regex so you can't use it in this situation.
You could change it to:
[a-zA-Z]+L
then anything like:
press LinkifyL or on AndroidL to search it on google
will change to a link, get it?
For allow user only can input lower case alphabet and number, how can I setup my EditText in a xml file ?
Sorry, I did not provide clear information. I just want to filter or check user's input.
AFAIK, your question is complicated to do. better get a input from user. then you can convert that string to lowercase using toLowerCase().