我需要在用户输入时屏蔽数字,因此在用户界面上,用户应该看到一个屏蔽数字,但是在Java代码上,我应该获得包括屏蔽字符在内的整个数字,即
用户应该看到的4545 ******** 9632
但是在Java代码(后面)上,我应该得到包括屏蔽字符在内的完整数字。我尝试将MaskFormatter
与JFormattedTextField
一起使用,但无法正常工作,它会显示整个数字。
try {
MaskFormatter mask=new MaskFormatter("####********####");
JFormattedTextField js=new JFormattedTextField();
mask.install(js);
} catch (ParseException ex) {
Logger.getLogger(Masker.class.getName()).log(Level.SEVERE, null, ex);
}
答案 0 :(得分:1)
这是我的判断,
class ACustomJEditText extends JTextField{
ArrayList<String> realText=new ArrayList<String>();
String displayText="";
public ACustomJEditText() {
// TODO Auto-generated constructor stub
super();
addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
// DELETE TEXT on backspace
if(e.getKeyCode()==KeyEvent.VK_BACK_SPACE) {
if(realText!=null && realText.isEmpty()==false) {
realText.remove(realText.size()-1);//remove character
}
turnRealTextToString();
//set the display text here
setText(displayText);
return;
}
//avoid any input if string actually string size is greater than 16
if(realText.size()==16) {
setText(displayText);
return;
}
//other keys should now be added to the input for only numbers
try{
int input=Integer.parseInt(e.getKeyChar()+"");
//add int to realtext
realText.add(input+"");
//turn real text to ####********#### string
turnRealTextToString();
setText(displayText);
}catch (Exception ex) {
// Other keys fail.
setText(displayText);
}
}
private void turnRealTextToString() {
String result="";
for(int i=0;i<realText.size();i++) {
if(i>3 && i<12) {
result+="*";
}else {
result+=realText.get(i);
}
}
String realDisplay=realText.toString();
System.out.println("DISPLAY: "+result+" REAL: "+getRealText());
//set result to display text
displayText=result;
setText(displayText);
}
});
}
//get the actual real text
public String getRealText() {
StringBuilder real=new StringBuilder();
realText.forEach(text->{
real.append(text);
});
return real.toString();
}
}
应该像魔术一样工作。
答案 1 :(得分:0)
您可以使用正则表达式:
var cardnumber = '4567 6365 7987 3783';
var first4 = cardnumber.substring(0, 4);
var last5 = cardnumber.substring(cardnumber.length - 5);
mask = cardnumber.substring(4, cardnumber.length - 5).replace(/\d/g,"*");
console.log(first4 + mask + last5);
或者如果您可以尝试以下操作:
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
<input class="form-control" id="myinput" data-length="12" name="name">
</body>
</html>
<script>
let initial = 4;
let maskChars = 6;
let realNumber = "";
$(function() {
$('#myinput').keyup(function(e) {
realNumber += this.value[this.value.length-1];
if (this.value.length >= initial && this.value.length <= initial + maskChars) {
this.value = this.value.slice(0, -1) + '*';
}
});
});
</script>
这就是我处理JS的方式。想法是一样的。如果长度大于所需的数字长度,则可以用*替换字符。我没有尝试过此Java代码。有关更多信息,请访问以下链接: https://www.javacodeexamples.com/mask-part-of-string-example-java/878
package com.javacodeexamples.stringexamples;
import org.apache.commons.lang3.StringUtils;
public class MaskStringExample {
public static void main(String[] args) throws Exception {
String str = "1234567812345678";
//mask first 4 characters
System.out.println( maskString(str, 0, 4, '*') );
//mask everything but last 4 digits
System.out.println( maskString(str, 0, 12, '*') );
//mask everything
System.out.println( maskString(str, 0, str.length(), '*') );
//mask everything but first and last 4 digits
System.out.println( maskString(str, 1, 12, '*') );
}
private static String maskString(String strText, int start, int end, char maskChar)
throws Exception{
if(strText == null || strText.equals(""))
return "";
if(start < 0)
start = 0;
if( end > strText.length() )
end = strText.length();
if(start > end)
throw new Exception("End index cannot be greater than start index");
int maskLength = end - start;
if(maskLength == 0)
return strText;
String strMaskString = StringUtils.repeat(maskChar, maskLength);
return StringUtils.overlay(strText, strMaskString, start, end);
}
}