Desetinná čárka vs desetinná tečka – validace

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
pta
Člen | 11
+
0
-

Čaute,

hledal jsem, jestli to tu už nekdo řešil a nenašel jsem nic.

Jak řešíte uživatelskou pohodlnost v případe vkládání desetinných čísel, kdy je nutné akceptovat jako oddělovač desetinných míst čárku i tečku bez vyhození chybové hlášky?

Zajímá mě hlavně, jestli k tomu existuje client-side podpora. Server-side custom validator bych si na to napsal.

Diky.

Editoval pta (25. 11. 2009 19:05)

Honza Kuchař
Člen | 1662
+
0
-

Mám oboje. Na client side jsem použil: http://code.google.com/…erformatter/ Funguje to bezvadně. Ale pokud tam člověk zadá vyloženě nesmysl, tak to vyhodí NaN,NaN. Ale to se určitě dá ošetřit.

David Grudl
Nette Core | 8111
+
0
-

Validátor Form::FLOAT by měl akceptovat tečku i čárku. Nefunguje?

pta
Člen | 11
+
0
-

Form::FLOAT sice vezme tecku i carku, ale v pripade carky desetinnou cast nebere: kdyz zadam 1,23 tak z toho udela 1.00. Jeste pro jistotu znovu vyzkousim.

Honza Kuchař
Člen | 1662
+
0
-

Já jsem si na to udělal vlastní input:

<?php



/**
 * NumericInput input control.
 *
 * @author     Jan Kuchař
 * @package    Nette\Extras
 */
class NumericInput extends /*Nette\Forms\*/TextInput
{
	/*
	 * Group separator
	 * Example: 1,000.00 - there it is ,
	 * @var string
	 */
	static public $groupSeparator = " ";

	/*
	 * Decimal separator
	 * Example: 1,000.00 - there it is .
	 * @var string
	 */
	static public $decimailSeparator = ",";

	/*
	 * Accuracy
	 * Example: 1,000.00 - there it is 2
	 * @var string
	 */
	static public $decimals = 2;


	protected static function _fixString($string) {
		$string = str_replace(self::$groupSeparator, "", $string);
		$string = str_replace(self::$decimailSeparator, ".", $string);// Do amerického formátu
		return $string;
	}

	/**
	 * @param  string  label
	 * @param  int  width of the control
	 * @param  int  maximum number of characters the user may enter
	 */
	public function __construct($label = NULL, $cols = NULL, $maxLenght = NULL)
	{
		parent::__construct($label, $cols, $maxLenght);
		$this->control->class = array('number');
		$this->addFilter("TextInput::filterFloat");
	}

	/**
	 * Getts value
	 * @return float
	 */
	public function getValue() {
		return (float)parent::getValue();
	}

	/**
	 * Generates control's HTML element.
	 * @return Html
	 */
	public function getControl()
	{
		$control = parent::getControl();
		$control->value = number_format($this->getValue(), self::$decimals, self::$decimailSeparator, self::$groupSeparator);
		return $control;
	}

	public static function validateSmaller(IFormControl $control, $num)
	{
	    return ($control->getValue()<$num);
	}


	public static function validateBigger(IFormControl $control, $num)
	{
	    return ($control->getValue()>$num);
	}

}

a jako JavaScript:

	$("input.number").livequery(function () {
		$(this).bind("blur",function(){
			$(this).format({format:"#,###.00", locale:"cz"});
		}).blur();
	})