How to Get Int Instead String from Form

How to get int instead string from form?

// convert the $_POST['a'] to integer if it's valid, or default to 0
$int = (is_numeric($_POST['a']) ? (int)$_POST['a'] : 0);

You can use is_numeric to check, and php allows casting to integer type, too.

For actual comparisons, you can perform is_int.

Update

Version 5.2 has filter_input which may be a bit more robust for this data type (and others):

$int = filter_input(INPUT_POST, 'a', FILTER_VALIDATE_INT);

I chose FILTER_VALIDATE_INT, but there is also FILTER_SANITIZE_NUMBER_INT and a lot more--it just depends what you want to do.

How to get int instead string from form?

// convert the $_POST['a'] to integer if it's valid, or default to 0
$int = (is_numeric($_POST['a']) ? (int)$_POST['a'] : 0);

You can use is_numeric to check, and php allows casting to integer type, too.

For actual comparisons, you can perform is_int.

Update

Version 5.2 has filter_input which may be a bit more robust for this data type (and others):

$int = filter_input(INPUT_POST, 'a', FILTER_VALIDATE_INT);

I chose FILTER_VALIDATE_INT, but there is also FILTER_SANITIZE_NUMBER_INT and a lot more--it just depends what you want to do.

How to get a String value from an EditText Int?

Not sure if this is related to Corda in anyways as its tagged corda.

However, I will try to answer this as it seems to be a generic Java question. To convert a String to an Integer you could use Integer.valueOf(<String>).

So for the above, it becomes note.setOraIn(Integer.valueOf(txtOraIn.getText().toString())).

Also, mathematic operations on String is not possible (in plain Java), unless you write some code to read the values and do the calculations in the background.

HTML input type= number still returning a string when accessed from javascript

It's normal you get a string.

The purpose of the number type is that mobile browsers use this for showing the right keyboards and some browsers use this for validation purposes. For example the email type will show a keyboard with the @ and '.' on the keyboard and number will show a numeric keyboard.

How do I convert a String to an int in Java?

String myString = "1234";
int foo = Integer.parseInt(myString);

If you look at the Java documentation you'll notice the "catch" is that this function can throw a NumberFormatException, which you can handle:

int foo;
try {
foo = Integer.parseInt(myString);
}
catch (NumberFormatException e) {
foo = 0;
}

(This treatment defaults a malformed number to 0, but you can do something else if you like.)

Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a powerful and concise way to convert a string into an int:

import com.google.common.primitives.Ints;

int foo = Optional.ofNullable(myString)
.map(Ints::tryParse)
.orElse(0)

Preventing a String from getting integer values in Java

You can use a regular expression on the string to see if it contains number characters:

if (Pattern.compile("[0-9]").matcher(sql_name).find()) {
// Show error message
}

I want my form input as an integer... but only if it was submitted as such

You might want to take a look at the ctype_digit() function (quoting) :

bool ctype_digit ( string $text )

Returns TRUE if every character
in the string $text is a decimal
digit, FALSE otherwise.

Using this to test what the input is made of, you should then be able to decide what to do with it, depending on the fact it contains an integer or not.


I suppose something like this should do the trick :

if (ctype_digit($_POST['your_field'])) {
// it's an integer => use it
} else {
// Not an integer
}


Related Topics



Leave a reply



Submit