I have User registration page in Phonegap www folder.I want to validate email id and phone number using native android functions.How do I do that?
Asked
Active
Viewed 204 times
2
-
Please refer this link - http://stackoverflow.com/questions/6358380/phone-number-validation-android & http://stackoverflow.com/questions/5958665/validation-for-a-cell-number-in-android- for phone number detection. – AkashG Nov 27 '12 at 06:01
-
And this link for mail validation- http://stackoverflow.com/questions/9355899/android-email-edittext-validation & this blog will also help you - http://chandan-tech.blogspot.in/2010/11/validating-email-address-pattern-in.html , http://www.technotalkative.com/android-validate-email-address/ – AkashG Nov 27 '12 at 06:03
1 Answers
0
We have to write JS interface to execute java code from javascript.
Write following code in your onCreate method:
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Object()
{
public void performClick(String emailAdress, String phoneNo)
{
// Validate email address and Phone number here
}
},"MyAndroid");
}
}
To call JS interface in phonegap app, write javascript function:
<script>
document.getElementById("validateButton").addEventListener('touchstart',touchPagePressed);// you can use 'onclick' also
function touchPagePressed()
{
// MyAndroid interface will execute java's performClick method
MyAndroid.performClick($("#emailId").val(),$("#phoneNo").val());
}
</script>

Mayur Birari
- 5,837
- 8
- 34
- 61
-
Can you please explain why to use webview in java class and how to declare webview in xml file – Shweta Nov 28 '12 at 07:10
-
http://docs.phonegap.com/en/2.2.0/guide_cordova-webview_android.md.html go thr with this document, you'll get it – Mayur Birari Nov 28 '12 at 07:15
-
My index.html file contains the view with necessary widgets.Is it necessary to declare webview in MainActivity? – Shweta Nov 28 '12 at 07:57
-
To access webView setting, you have to get the instance of WebView then only you can execute javascript interface. – Mayur Birari Nov 28 '12 at 08:29
-
-
yes, it is in main.xml, check the above link in detail, you'll get the clear picture. – Mayur Birari Nov 28 '12 at 08:56
-
It is different from wat u have given above.Here in activity extending Droidgap but in the phonegap link they are extending cordova interface. Am using cordova 5.0.am facing issues – Shweta Nov 28 '12 at 09:03
-
-
`WebSettings webSettings = super.getSettings();`, plz try super.getSettings() – Mayur Birari Nov 28 '12 at 10:56