Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the regular expression for valdating a 'price' in a jtextfield
    primarykey
    data
    text
    <p>I want to validate a jtextfield as user inputs a price of some item.</p> <p>I have a perfectly working code which beeps and discard if user inputs some character that doesnt match the regular expression.(for regular expression "[0-9]+" it accepts any number of digits and beeps if a letter is given as input)</p> <p>But the problem is I want to validate an input like "12500.00". Format should be any number of digits before decimal point and two digits after the decimal point. eg: ddd.dd</p> <p>I tried the following regular expression. But the i doesnt allow user to input "." character. It allows user to input any number of digits but when "." key is pressed input is not accepted.</p> <p>here is the regular expression I used. "[0-9]+ | [0-9]+. | [0-9]+.[0-9]{1} | [0-9]+.[0-9]{2}"</p> <p>important: user input is validated character by character as the user inputs them to the jtextfield.</p> <p>Here is the code</p> <pre><code>import java.awt.Toolkit; import java.util.regex.Pattern; import javax.swing.JComponent; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; public abstract class ValidatedTextField extends PlainDocument { private int max; private String regExp; public ValidatedTextField(int max, String regExp) { this.max = max; this.regExp = regExp; } @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { Pattern DIGITS = Pattern.compile(regExp); // System.out.println(str); if (getLength() + str.length() &gt; max) { str = str.substring(0, max - getLength()); Toolkit.getDefaultToolkit().beep(); } if (!DIGITS.matcher(str).matches() || str == null) { str = null; Toolkit.getDefaultToolkit().beep(); } if (str != null &amp;&amp; !passDependency(str)) { str = null; Toolkit.getDefaultToolkit().beep(); } //str != null &amp;&amp; DIGITS.matcher(str).matches super.insertString(offs, str, a); } public abstract boolean passDependency(String str); public void shiftFocus(JComponent jc, boolean condition) { if (condition) { jc.requestFocusInWindow(); } } } </code></pre> <hr> <p>Below is not the exact code but this is how I use it.</p> <pre><code>import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.*; public class ValidateTest { void makeFrame(){ JFrame jf=new JFrame("Verifier Test"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField text = new JTextField(); JLabel lbl=new JLabel("Price"); text.setDocument(new ValidatedTextField(10, "[0-9]+") { @Override public boolean passDependency(String str) { return true; } }); text.setPreferredSize(new Dimension(200, 30)); lbl.setPreferredSize(new Dimension(60, 30)); jf.getContentPane().add(text, BorderLayout.EAST); jf.getContentPane().add(lbl, BorderLayout.WEST); jf.pack(); jf.setVisible(true); text.setVisible(true); } public static void main(String[] args) { new ValidateTest().makeFrame(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload