Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Validation

Validation

Scheduled Pinned Locked Moved Java
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hitesh kalra
    wrote on last edited by
    #1

    I want that in textbox if 0 is insert than it shows an alert message. The textbox insert numbers only. How will I validate it? please help thanx in advance.

    4 1 Reply Last reply
    0
    • H hitesh kalra

      I want that in textbox if 0 is insert than it shows an alert message. The textbox insert numbers only. How will I validate it? please help thanx in advance.

      4 Offline
      4 Offline
      4277480
      wrote on last edited by
      #2

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class Validate extends JFrame
      {
      TextField t;
      public Validate()
      {
      t= new TextField(10);
      add(t);
      t.addKeyListener(new KeyAdapter()
      {
      public void keyPressed(KeyEvent e)
      {
      int key = e.getKeyCode();
      if ((key == KeyEvent.VK_0))
      {
      JOptionPane.showMessageDialog(null,"0 Was Clicked", "Note",JOptionPane.INFORMATION_MESSAGE);
      }
      else
      if((key == KeyEvent.VK_1)||(key == KeyEvent.VK_2)||(key == KeyEvent.VK_3)||(key == KeyEvent.VK_4)||(key == KeyEvent.VK_5)||(key == KeyEvent.VK_6||(key == KeyEvent.VK_7)||(key == KeyEvent.VK_8)||(key == KeyEvent.VK_9)))
      {
      return;
      }
      else
      {
      JOptionPane.showMessageDialog(null,"Invalid Input", "Note",JOptionPane.WARNING_MESSAGE);
      String s = t.getText();
      t.setText(s.replaceAll(e.getKeyChar()+"",""));

      				}
      		}
      	}
      	);
      	setTitle("Validate");
      	setLocationRelativeTo(null);
      	pack();
      	setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
      	setVisible(true);
      }
      public static void main(String\[\] args) 
      {
      	new Validate();
      }
      

      }

      D 1 Reply Last reply
      0
      • 4 4277480

        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;

        public class Validate extends JFrame
        {
        TextField t;
        public Validate()
        {
        t= new TextField(10);
        add(t);
        t.addKeyListener(new KeyAdapter()
        {
        public void keyPressed(KeyEvent e)
        {
        int key = e.getKeyCode();
        if ((key == KeyEvent.VK_0))
        {
        JOptionPane.showMessageDialog(null,"0 Was Clicked", "Note",JOptionPane.INFORMATION_MESSAGE);
        }
        else
        if((key == KeyEvent.VK_1)||(key == KeyEvent.VK_2)||(key == KeyEvent.VK_3)||(key == KeyEvent.VK_4)||(key == KeyEvent.VK_5)||(key == KeyEvent.VK_6||(key == KeyEvent.VK_7)||(key == KeyEvent.VK_8)||(key == KeyEvent.VK_9)))
        {
        return;
        }
        else
        {
        JOptionPane.showMessageDialog(null,"Invalid Input", "Note",JOptionPane.WARNING_MESSAGE);
        String s = t.getText();
        t.setText(s.replaceAll(e.getKeyChar()+"",""));

        				}
        		}
        	}
        	);
        	setTitle("Validate");
        	setLocationRelativeTo(null);
        	pack();
        	setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
        	setVisible(true);
        }
        public static void main(String\[\] args) 
        {
        	new Validate();
        }
        

        }

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        Would it not be simpler to use a JFormattedTextField to ensure numeric input and an InputVerifier to check the value is not 0?

        4 1 Reply Last reply
        0
        • D David Skelly

          Would it not be simpler to use a JFormattedTextField to ensure numeric input and an InputVerifier to check the value is not 0?

          4 Offline
          4 Offline
          4277480
          wrote on last edited by
          #4

          True there are many ways to do it. Snippet 1: Integers

          public Validate()
          {
          Text.addKeyListener(new KeyAdapter()
          {
          public void keyTyped(KeyEvent e)
          {
          char c = e.getKeyChar();
          if (!(Character.isDigit(c) ||(c == KeyEvent.VK_BACK_SPACE) ||(c == KeyEvent.VK_DELETE)))
          {
          getToolkit().beep();
          e.consume();
          }
          if( c == KeyEvent.VK_0)
          {
          Label.setText("0 entered");
          Label.setForeground(Color.red);
          }
          else
          {
          Label.setText("ok");
          Label.setForeground(Color.black);
          }
          }
          });

          Snippet 2: Double

          public class Validate()
          {
          DoubleValidate dv = new DoubleValidate();
          public Validate()
          {
          Text.setDocument(dv);
          add(Text);
          }

          static class DoubleValidate extends PlainDocument 
          {
          	public void insertString(int offset,String string, AttributeSet attributes)throws BadLocationException 
          	{
          		if (string == null) 
          		{
          			return;
          		} 
          		else 
          		{
          			String newValue;
          			int length = getLength();
          			
          			if (length == 0) 
          			{
          				newValue = string;
          			} 
          			else 
          			{
          				String currentContent = getText(0, length);
          				StringBuffer currentBuffer = new StringBuffer(currentContent);
          				currentBuffer.insert(offset, string);
          				newValue = currentBuffer.toString();
          			}
          			try 
          			{
          				Double.parseDouble(newValue);
          				super.insertString(offset, string,attributes);
          			} 
          			catch (NumberFormatException exception) 
          			{
          				Toolkit.getDefaultToolkit().beep();
          			}
          		}
          	}
          }
          

          }

          D 1 Reply Last reply
          0
          • 4 4277480

            True there are many ways to do it. Snippet 1: Integers

            public Validate()
            {
            Text.addKeyListener(new KeyAdapter()
            {
            public void keyTyped(KeyEvent e)
            {
            char c = e.getKeyChar();
            if (!(Character.isDigit(c) ||(c == KeyEvent.VK_BACK_SPACE) ||(c == KeyEvent.VK_DELETE)))
            {
            getToolkit().beep();
            e.consume();
            }
            if( c == KeyEvent.VK_0)
            {
            Label.setText("0 entered");
            Label.setForeground(Color.red);
            }
            else
            {
            Label.setText("ok");
            Label.setForeground(Color.black);
            }
            }
            });

            Snippet 2: Double

            public class Validate()
            {
            DoubleValidate dv = new DoubleValidate();
            public Validate()
            {
            Text.setDocument(dv);
            add(Text);
            }

            static class DoubleValidate extends PlainDocument 
            {
            	public void insertString(int offset,String string, AttributeSet attributes)throws BadLocationException 
            	{
            		if (string == null) 
            		{
            			return;
            		} 
            		else 
            		{
            			String newValue;
            			int length = getLength();
            			
            			if (length == 0) 
            			{
            				newValue = string;
            			} 
            			else 
            			{
            				String currentContent = getText(0, length);
            				StringBuffer currentBuffer = new StringBuffer(currentContent);
            				currentBuffer.insert(offset, string);
            				newValue = currentBuffer.toString();
            			}
            			try 
            			{
            				Double.parseDouble(newValue);
            				super.insertString(offset, string,attributes);
            			} 
            			catch (NumberFormatException exception) 
            			{
            				Toolkit.getDefaultToolkit().beep();
            			}
            		}
            	}
            }
            

            }

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            Yes, there are many ways to make something more complicated than it needs to be.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups