Android: are there any good solutions how to validate Editboxes - android

For example, I want to Validate Minimum-Length, and if it's Email, a PhoneNumber.
How is this possible in android. I want to validate.

If you wan to prevent user to type something, then extend the InputFilter and register it with your EditText.
// built in InputFilter.LengthFilter limits the umber of chars
EditText.setFilters(new Filter[]{new InputFilter.LengthFilter(100)})

There are a number of things that you can do to validate
Add input filters. More on it is here http://developer.android.com/reference/android/text/InputFilter.html How to add filter to editable view is mentioned here http://developer.android.com/reference/android/text/Editable.html#setFilters%28android.text.InputFilter
Use TextWatchers to modify the content on the go. More on TextWatchers is here http://developer.android.com/reference/android/text/TextWatcher.html Set this up for your EditText using http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)
Note: There are few of them implemented in Android itself. Make use of them if you can. Look for subclasses in the documentation for TextWatcher and InputFilter

For the validation of Text box
1. minimum length: u can directly give the length of that text.
2. mail validation:
public boolean isEmailValid(String email)
{
String regExpn =
"^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))#"
+"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
CharSequence inputStr = email;
pattern = Pattern.compile(regExpn,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(inputStr);
if(matcher.matches())
return true;
else
return false;
}
1. For phone number: If you want to fix length then give the length and the input type is number.

Also take a look here :) Android: Validate an email address (and more) in EditText

Related

Using string.all with regex

I am trying to check if a string has all the forbidden characters like "/#$%^&" ...
i tried to find a solution but couldn't find anything , i just want to check if all characters in the string match regex pattern \w
string.all seems perfect but i cant add regex pattern to it
here is what i am trying to do:
// "abce#ios" must return false because it contains #
// "abcdefg123" must return true
fun checkForChars(string :String) :Boolean {
val pattern = "\\w".toRegex()
return (string.contains(regex = pattern))
}
thanks in advance
You don't need to use regex at all with all:
fun checkForChars(string: String): Boolean = string.all(Char::isLetterOrDigit)
You made several mistakes:
\w pattern matches exactly one letter, if you want to match zero or more letters you need to change it to: \w*
Instead of checking whether the string contains the regex, you need to check if it matches the regex.
The final solution is the following:
fun checkForChars(string :String) :Boolean {
val pattern = "\\w*".toRegex()
return (string.matches(pattern))
}
You can use
Regex("[^/#$%^&]*").matches(string) to check for the forbidden characters.
You can include any forbidden characters into a [^...]* construction. Though a " character would need to be screened and a \ character would need to be screened twice. Regex("[^\\\\/#$%^&\"]*").
For \\w* regex you can use Regex("\\w*").matches(string)

Input type = Registration number

How do I set the input type for registration number. For example my university has specific format for registration number i.e. CIIT/SP17-mcs-044/Atk. I want CIIT/ATK is written already and Sp17-mcs-044 as a hint.
You can't define a random types of input type. You can only have one of these types as inputType.
If you want any custom behaviour, you might use TextWatcher and validate it
As #Tim has stated, the input type would be text over here and when user types in that EditText then check if the input is correct by using RegEx, and then show any message when the input is correct, for that you will need to add an OnTextChangedListener, about which you can learn more on official documentation.
Now, CIIT/ATK can be a TextView placed right before the EditText so it will be written already and will be uneditable. And yes, add that in final string after getting the input, like this:
finalInput = "CIIT/ATK" + input;

Android EditText - Capitalize first letter of sentences using setText()

Folks,
I need to capitalize first letter of every sentence. I followed the solution posted here
First letter capitalization for EditText
It works if I use the keyboard. However, if I use setText() to programatically add text to my EditText, first letter of sentences are not capitalized.
What am I missing? Is there a easy way to fix or do I need to write code to capitalize first letters in my string before setting it to EditText.
The only thing the inputType flag does is suggest to the input method (e.g. keyboard) what the user is attempting to enter. It has nothing to do with the internals of text editing in the EditText view itself, and input methods are not required to support this flag.
If you need to enforce sentence case, you'll need to write a method which does this for you, and run your text through this method before applying it.
You can use substring to make this
private String capSentences( final String text ) {
return text.substring( 0, 1 ).toUpperCase() + text.substring( 1 ).toLowerCase();
}
Setting inputType doesn't affect anything put into the field programmatically. Thankfully, programmatically capitalizing the first letter is pretty easy anyway.
public static String capFirstLetter(String input) {
return input.substring(0,1).toUpperCase() + input.substring(1,input.length());
}

Android EditText, show both comma and dot as possible separator in case of numberDecimal input type

I need to let user choose between two variants when he inputs decimal number:
use comma(,) as separator
use dot(.) as separator
By default if I use inputType="numberDecimal" in the EditText xml config - EditText shows only digits and comma (,) as possible separator.
I've tried to use android:digits="0123456789, in my EditText config, but without result - EditText widget shows just digits and comma.
I want to have both variants (. and ,) available for user on on-screen keyboard when he tries to input decimal number.
Could you please advise?
Specifying both android:inputType="numberDecimal" and android:digits="0123456789,." will display a keyboard with mostly just numbers and punctuation (depending on the user's chosen keyboard). It will also limit the characters accepted as input to those in your digits attribute.
If you'd like to further limit the keyboard to display only certain characters, you'll need to define a custom one. Here's one of the better tutorials on doing that.
Use proper validation. Let the user see full keyboard but he remain aloof of using it. Means user should not be able to use or input anything using keyboard.
etlocation = (EditText) findViewById(R.id.etlocation);
and used this
etlocation.getText().toString();
if (!isValidLocation(etlocation.getText().toString().trim()))
{
etlocation.setError("Invalid location");
}
validate this
public static boolean isValidLocation(String str) {
boolean isValid = false;
String expression = "^[0-9,.]*$";
CharSequence inputStr = str;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
You can read this link and also read allow only number and period(.) in edit text in android. Hopefully are helpful from those links. Best of Luck!
You can create a custom keyboard.
The below link shows a nice example of custom keyboard
http://www.mediafire.com/download/39q7of884goa818/myKeyborad2.zip
and check this link also
How to develop a soft keyboard for Android?

The use of input type in EditText

Even if I set the input type to numberdecimal or number, I have to cast the number to get the number. Then what is the use of input type in EditText views.
e.g.
int a = Integer.valueOf(editText.getText().toString());
One more thing, why do I need to use toString() with almost every views to get Text? In java, we could just getText anything from controls.
You have to parse Text to Integer because it doesn't return int. It returns Editable formatted by input type. So if you set input type to numbers you get Editable which contains only numbers.
And you have to add toString() because EditText return Editables not Strings.
InputType is used for various purposes. For example, in a password field, it can hide the characters.
Here's the official description of every single property it can take:
https://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
As for the second part of your question, EditText.getText() returns Editable
This defines a common interface for all text whose content and markup can be changed (as opposed to immutable text like Strings).
So you need to use toString() to get a string out of it.

Categories

Resources