Results 1 to 3 of 3
  1. #1
    tanalyw is offline Member
    Join Date
    Apr 2008
    Posts
    4
    Rep Power
    0

    Default How do check I allow user to enter only alpha numeric and -?

    Hi another question

    I need to do a prompt on console.
    However, the prompt only accepts alpha numerical values and dash (-)
    How do i go about it.

    I only manange to check if the input user entered is alpha-numeric using this method,

    boolean true = username.matches("\\p{Alnum}+");

    how do i prevent user from entering funny characters like )(*&^%$#@! except for - ? thanks

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    I think this is what you're looking for.

    Java Code:
    if (!test.matches("[\\p{Alnum}[\\-]]*"))
    {
    //error
    }
    else
    {
      //your ok
    }
    Last edited by Chris.Brown.SPE; 04-15-2008 at 12:46 PM.

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Try this algorithm, ( in a GUI "textfield" )

    Before the user inputs, save the current value of textfield on a variable.
    in every input, check the last character for decimal and dash
    if satisfied, allow and replace the saved value to this new one.
    else, remove the text, and replace it with the value that was stored.

    kind regards,
    sukatoa

Similar Threads

  1. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM
  2. Applet - enter URL and view the page
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-10-2008, 02:38 PM
  3. Allowing only numeric values in a TextField
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:08 PM
  4. enter data from an optic device
    By bbq in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 08:02 AM
  5. Javascript and enter button
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-11-2007, 09:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •