Turn on thread page Beta
    • Thread Starter
    Offline

    0
    ReputationRep:
    i'm currently developing a web based form that can calculate some operation (using JSP), can i programmed it so that only integer and double values can be inserted as input? (if the user inputs char or any other symbol, the page can simply refuse it)
    Offline

    0
    ReputationRep:
    google it! lol...search for something like JSP form validation or javascript form validation.

    here's one I found. I'm sure you'll be able to port it to JSP.

    function IsNumeric(sText)

    {
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++)
    {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
    IsNumber = false;
    }
    }
    return IsNumber;

    }
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by thebigdino)
    google it! lol...search for something like JSP form validation or javascript form validation.

    here's one I found. I'm sure you'll be able to port it to JSP.

    function IsNumeric(sText)

    {
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++)
    {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
    IsNumber = false;
    }
    }
    return IsNumber;

    }

    lol google was my 1st option which it give me = javascript :p:
    is there any function via JSP or Java directly?

    but thanks though
    Offline

    0
    ReputationRep:
    In Java, use Integer.parseInt("123"), and then if it fails (i.e. it wasn't just numbers), it will throw a NumberFormatException. That's probably the best way I can think of right now...
    Offline

    17
    ReputationRep:
    Except you want to use Double.parseDouble() as the OP wants double values too.

    If you just use javascript you could do a regular expression check looking for either numbers or numbers followed by a decimal point followed by numbers.

    You could do the regular expression in the java code too, but the parseDouble() is just easier.
    Offline

    0
    ReputationRep:
    In terms of programming logic you can use a simple string code manipulation, study how a string manipulation would detect whether it is a number , character,string or alphanumeric figures. By capturing the logical sequence you can adopt the method in your current program.
 
 
 
Poll
Black Friday: Yay or Nay?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.