Validation of Name in EditText - android

In my application I have field name. It works perfect if field contain digits or .,?... characters but when I enter usman(space)shafi it gives error. My statement take space as invalid character between two words. Please can anyone tell me how to make it work. Thanks
here is the code
if(!Pattern.matches("[a-zA-Z]+", textname.getText().toString().trim())){
textname.setError("Invalid Characters");
}
// if(!Pattern.matches("[a-zA-Z]+", textkin.getText().toString().trim())){
// textkin.setError("Invalid Characters");
// }");

Add the space to your regular expresion:
if(!Pattern.matches("[a-zA-Z ]+", textname.getText().toString().trim())){
textname.setError("Invalid Characters");
}

If it would be just a one white space inside you can try something like this:
text = textname.getText().toString().trim();
string[] txtSplt = text.split(" ");
text = "";
foreach (string s : txtSplt)
{
text += s;
}
Then you can proceed with checking the condition without changing your regex.

DO this by this way. Your problem will be solved.
String regex = "([A-Z a-z]+)";
Pattern pattern1 = Pattern.compile(regex);
Matcher matcher1 = pattern1.matcher(textname.getText().toString());
if(!matcher1.matches()){
textname.setError("Invalid Characters");
}

Related

Ignore starting blank space and special character only capitalize first alphabet from string

String name1 = " shashi";
Output: name1:" Shashi";
String name2 = "###shashi";
Output: name2: = "###Shashi";
String name3 = "##$&shashi";
Output: name3: = "##$&Shashi";
Note: Capitalize only first letter of alphabet, ignore space and special character.
Try this to remove Special Characters
public static String getOnlyStrings(String s) {
Pattern pattern = Pattern.compile("[^a-z A-Z]");
Matcher matcher = pattern.matcher(s);
String number = matcher.replaceAll("");
return number;
}
So your call should be
str = getOnlyStrings(str);
& then capitalized first letter using
str.replace(str.charAt(0),str.toUpperCase().charAt(0));
You may need to change the Pattern according to your needs, Current pattern only accepts Characters from a to z
Credits : Answer: How to remove special characters from a string?
str.replaceAll(" ","");
str.replace(str.charAt(0),str.toUpperCase().charAt(0));
hm..... if you want to ignore the special character, recommend to use ASCIICODE and charAt method.

How to ignore spaces between text editview Android

I am trying to ignore spaces in editview between text, I am not quite sure how I can go about doing this. I know I can use trim feature to ignore spaces before and after the full text but how do I ignore space between strings if there is any;
String myTextEdited myText.getText().toString().trim();
For example, if I have / user types in this;
Allan Bob
3523 JKO
NY1 U90
I want to ingore spaces when I read this in my if statement or put it in another variable for example;
String name = "AllanBob"
For example, to ignore upper and lower cases I am doing this;
if (myText.getText().toString().trim().equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
What I would like to do is add another feature in here that also ignores spaces before, between and after text e.g. instead of;
myname is Henry . (space until here)
It should read it as mynameishenry but to the user it still appears as they have written it.
Please let me know if my question was not clear, I will try explaining it better
EDITED:
is it possible to ignore spaces in string that I have inside my if statement. For example;
if (myText.getText().toString().trim().equalsIgnoreCase("Henry 0887")) {
// do something
} else {
// do something
}
but currently if the user types in henry0887, the if statement does not validate it because I added a space inside my validation text and therefoe its looking for a space in the text, is it possible to over come this, so even if I have space inside my validation it ignores it.
Did you try this:
String myString = myEditText.getText().toString();
myString = myString .replace(" ", "");
Hope it helps
EDIT:
if (myText.getText().toString().replace(" ", "").equalsIgnoreCase(userInput) || myText.getText().toString().equalsIgnoreCase(userInput)) {...
Try this,
if(myText.getText().toString().trim().replace(" ","").equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
Hope this helps.
use replaceAll() method.
str = str.replace(" ","");
or for all space chars:
str = str.replace("\\s+","");
EDIT
if (myText.getText().toString().replace("\\s+","").equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
EDIT2
if (myText.getText().toString().replace("\\s+","").equalsIgnoreCase("Henry 0887".replace("\\s+",""))) {
// do something
} else {
// do something
}

how to get text with using substring

I try to get only this part "9916-4203" in "Region Code:9916-4203 " in android. How can I do this?
I tried below code, I used substring method but it doesn't work:
firstNumber = Integer.parseInt(message.substring(11, 19));
If you know that string contains "Region Code:" couldn't you do a replace?
message = message.replace("Region Code:", "");
Assumed that you have only one phone number in your String, the following will remove any non-digit characters and parse the resulting number:
public static int getNumber(String num){
String tmp = "";
for(int i=0;i<num.length();i++){
if(Character.isDigit(num.charAt(i)))
tmp += num.charAt(i);
}
return Integer.parseInt(tmp);
}
Output in your case: 99164203
And as already mentioned, you won't be able to parse any String to Integer in case there are any non-digit characters
Im going to guess that what you want to extract is the full region code text minus the title. So maybe using regex would be a good simple fit for you?
String myString = "Region Code:9916-4203";
String match = "";
String pattern = "\:(.*)";
Pattern regEx = Pattern.compile(pattern);
Matcher m = regEx.matcher(myString);
// Find instance of pattern matches
Matcher m = regEx.matcher(myString);
if (m.find()) {
match = m.group(0);
}
Variable match will contain "9916-4203"
This should work for you.
Java code sourced from http://android-elements.blogspot.in/2011/04/regular-expressions-in-android.html
In Java the substring() method works with the first parameter being inclusive and the second parameter being exclusive. Meaning "Hello".substring(0, 2); will result in the string He.
In addition to excluding the parsing of something that isn't a number like #Opiatefuchs mentioned, your substring method should instead be message.substring(12, 21).

How do I match the pattern in android

I am currently working on an android project. I would like to know if a string contains the '\r' or '\n' as the last character. How to do the pattern match?
Try this.
String string = "stackoverflow";
lastchar = string.substring(string.length() - 1);
It will give you the result "w".
try
String patterntomatch ="^[_A-Za-z0-9-]*(\r\n)$";
Pattern pattern=Pattern.compile(patterntomatch);
Matcher matcher=pattern.matcher(matchfromedittext);
boolean matcher.matches();
String last = your_string.substring(Math.max(your_string.length() - 2, 0));
//It will give you the last 2 characters of the string. If the string is null
//or has less than 2 characters, then it gives you the original string.
if(last.equals("\r") || last.equals("\n")){
//String's last two characters are either \n or \r. Now do something.
}

android : app crash if user inputs two decimal points like "1..5" instead of "1.5"

i have an android program where i have successfully restricted the user to input only value after decimal.. the problem is if user inputs two decimal points like "1.." then the app crashes. i have allowed only one special character to be used i.e decimal point itself. so how can i restrict the user from entering two decimal points or else show some validation.
i need something like this
else if(txtLdays.getText().toString().trim().equals(".."))
{
txtLdays.requestFocus();
txtLdays.setError("Double decimal ?");
return false;
}
Try this..
Use contains like below
else if(txtLdays.getText().toString().trim().contains(".."))
{
txtLdays.requestFocus();
txtLdays.setError("Double decimal ?");
return false;
}
EDIT
String to double
double result = Double.parseDouble(txtLdays.getText().toString().trim());
int to double
double result = (double) 12;
If user enters 12.
then check endsWith
double result;
if(txtLdays.getText().toString().trim().endsWith("."))
result = Double.parseDouble(txtLdays.getText().toString().trim().replace("\\.", ""))
There are two possibilitites
if you want to restrict when any button pressed..
then replace all ".." characters with empty chars like..
String data=View.getText().toString().replaceAll("..", "");
2.if you restrict when the user is typing then write a TextWatcher listener for the Edittext and you need to peform some validations there...
Did you tried setting decimal input type ?
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal|number"
/>
Try this :
int count = StringUtils.countOccurrencesOf((txtLdays, ".");
if (count > 1){
txtLdays.requestFocus();
txtLdays.setError("Double decimal ?");
}
Please downlaod this jar file Commons Lang
The right way would be to match the value against a regular expression.
Here is an example that allows a string that starts/end with zero or more spaces and contains a double decimal point number:
//First make sure you avoid null pointer exception
if (txtLdays != null && txtLdays.getText() != null) {
String daysString = txtLdays.getText().toString();
String regex = "^\s*\d+\.\.\d+\s*$"
if (daysString.matches(regex)) {
txtLdays.requestFocus();
txtLdays.setError("Double decimal ?");
return false;
}
}
You can youse some kind of online regular expression tool to verify your matcher.
equals will check Object.
You need to check equalsIngnoreCase(); this ll check all the character
specially u need
else if(txtLdays.getText().toString().trim().indexOf("..") != -1 ){
txtLdays.requestFocus();
txtLdays.setError("Double decimal ?");
return false;
}

Categories

Resources