title: Validation allow only number and characters in edit text in android
body: <p>In my application I have to validate the <code>EditText</code>. It should only allow character, digits, underscores, and hyphens.</p>
  <p>Here is my code:</p>
  <pre><code>edittext.addTextChangedListener(new TextWatcher() {
                                          
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
          // TODO Auto-generated method stub                              
      }
                                          
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          // TODO Auto-generated method stub
      }
                                          
      @Override
      public void afterTextChanged(Editable s) {
          // validation codes here
                                  
          location_name=s.toString();
          Toast.makeText(getApplicationContext(),location_name, Toast.LENGTH_SHORT).show();                
          
          if (location_name.matches(&quot;.*[^a-z^0-9].*&quot;)) {
              location_name = location_name.replaceAll(&quot;[^a-z^0-9]&quot;, &quot;&quot;);
              s.append(location_name);
              s.clear();
              Toast.makeText(getApplicationContext(),&quot;Only lowercase letters and numbers are allowed!&quot;,Toast.LENGTH_SHORT).show();
          }                                   
      }
  
  });     
                                      
  location.add(location_name);
  </code></pre>
  <p>When I enter input into <code>EditText</code>, the application is force closed.</p>