欢迎来到天天文库
浏览记录
ID:10573908
大小:33.50 KB
页数:2页
时间:2018-07-07
《matches验证电话号码,java使用正则表达式验证电话号码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、验证电话号码packagecom.java2010.swing05;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JTextField;publicclassTest05extendsJFram
2、e{JLabeljl1;JTextFieldjtf;JButtonjb1;publicTest05(){jl1=newJLabel("电话号码:");jl1.setBounds(25,30,80,20);jtf=newJTextField();jtf.setBounds(90,30,130,25);jb1=newJButton("提交");jb1.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){//TOD
3、OAuto-generatedmethodstubdo_but(e);}});jb1.setBounds(90,100,80,25);this.setLayout(null);this.add(jl1);this.add(jtf);this.add(jb1);this.setVisible(true);this.setSize(300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoiddo_but(ActionEvente){Stringstr
4、=this.jtf.getText().trim();Stringinfo=check(str);JOptionPane.showMessageDialog(null,info);}//定义正则表达式publicStringcheck(Stringtext){if(text==null
5、
6、text.isEmpty()){return"请输入电话号码!";}//定义正则表达式规则Stringregex="^\d{3}-?\d{8}
7、\d{4}-?\d{8}$";//判断输入数据是否为电话号码if(text.matches(
8、regex)){returntext+"您的电话号码是合法的!";}else{returntext+"您的电话号码不合法的!";}}publicstaticvoidmain(String[]args){newTest05();}}
此文档下载收益归作者所有