server recognizes comma as a string - laravel

So basically i have an input type text which is validated to contain only numbers, comma and dot. When i enter(5.32) for example everything is okay but when i enter (5,32) it won't let me submit it, because it recognizes it as a string, which i don't want. How can i convert the comma into a dot in backend and still show as a comma ?
<input type="text" name="monthly_price" id="monthly_price" value="" class="comma-validation">
$('.comma-validation').on('input', function() {
var currentInput = $(this).val();
var fixedInput = currentInput.replace(/[^0-9 \,.]/, '');
$(this).val(fixedInput);
});

In your controller you can do:
$monthlyPrice = (float)str_replace(',', '.', request('monthly_price'));
This replaces the comma into a decimal. Then casts it as a float.

Related

React form hook - validation, no Trailing or Leading or only white spaces for input value

I'm trying to create a validator in "react-hook-form": "^7.3.6" #typescript with #material-ui for cases that includes whitespace in the input field; for example, a user can enter " "/" username"/" username " and it will be a valid username.
I have tried several Regular expressions in the pattern field but didn't come up with the right one for what I'm looking for, or am I not approaching this the right way?
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="username"
label="Enter Username"
name="username"
autoFocus
{...register("username", {
required: "required",
minLength: {
value: 3,
message: "minimum number of character for username is 3"
},
pattern: {
value: /\s/g,
message: "Entered value cant start/end or contain only white spacing"
},
})}
helperText={errors.username?.message}
/>
You probably want /^[^\s]+(?:$|.*[^\s]+$)/ for your regular expression, if you are trying to match valid inputs (/(?:^\s+|\s+$)/ will match the opposite, invalid inputs). /\s/g will catch any string that contains whitespace (e.g. match: "this string matches", no match: "this_string_doesn't").

JSTL Concatenate String to be used in html

how would I concatenate a string so It can be rendered in HTML properly?
<c:set var="filter" value="${view}"/>
For example, I do something like this in JavaScript:
var view;
var sors;
var filterCriteria = view + "<br>";
if (sors != null)
{
filterCriteria = filter + "SORs: " + sors + ", ";
}
Then the answer is
${view} <br> whatever
or
<c:out value="${view}"> <br> whatever
The first one doesn't escape the HTML special chars in view (<, >, &, ', "), whereas the second one does (and transforms them to <, >, &, etc.)
If you want to output <br> as html tag you could use c:out as <c:out escapeXml="false" value="${view} <br>"/>

Razor and function call outputting

The scenario is I would like to write a hidden field with a guid value generated by the server.
Why does
<input type="hidden" id="sampleGuid" value="#{Guid.NewGuid().ToString()};" />
yield 'value=""' while
#{
string token = Guid.NewGuid().ToString();
<input type="hidden" id="sampleGuid" value="#token" />
}
properly fill in 'value' with a guid?
You need parentheses instead of braces.
#{ ... } will execute ordinary statements, but won't print anything.
#(...) will print the value of an expression. (and will HTML-encode it)
You've wrapped Guid.NewGuid().ToString() in curly braces.
That just means you want to execute the code, not ouput it.
If you're trying to output a value, wrap the code in parenthesis.

get form values other than by name in codeigniter

hi i am using codeigniter . i have a form , there i add hidden fields dynamically . so every hidden field is <input type='hidden' name='hidden' value="+$(this).attr('title')+"> so the name is equal .
the problem is when i submit the form and try to get my hiden field values i can only get one hidden field value , because the names are same
i print my form values
print_r($this->input->post());
i have 2 hidden fields but i get only one
Array
(
[hidden] => march
[textbox] => march
[mysubmit] => Submit
)
i can change the name dynamically of hidden field when creating , but then i don't know exactly the name of my hidden field ,
how can i get hidden field values with same name ?? is there any way to get form values other than by name ?? i tried and can not find an answer , please help .............
You'll need to use brackets in your name attributes:
<input type='hidden' name='hidden[]'>
<!-- ^^^^ -->
This will allow PHP to accept multiple inputs with the same name as an array of values, so in this case, $_POST['hidden'] will return an array of strings.
By default they are indexed starting at 0, so $_POST['hidden'][0] will get you the first one, $_POST['hidden'][1] will get you the second, etc., however - you can explicitly index them if it's easier for you, either with numbers or strings.
<input type='hidden' name='hidden[first]'>
<input type='hidden' name='hidden[second]'>
Or:
<input type='hidden' name='hidden[0]'>
<input type='hidden' name='hidden[1]'>
You can nest these as deep as you want like hidden[first][1][], and they will be treated similarly to a PHP array when you get the $_POST values, but you need the brackets in the HTML.
Without brackets, only the last field's value will be available in the $_POST array. This is a PHP feature, Codeigniter can't do anything about it.

Xpath: how to select an option based on its text not value property?

consider both types:
<select name="garden">
<option>Flowers</option>
<option selected="selected">Shrubs</option>
<option>Trees</option>
<option selected="selected">Bushes</option>
<option>Grass</option>
<option>Dirt</option>
</select>
Is #val for actually indicating the value="" attribute ?
Is #value for indicating the innerText value ?
for example what happens if <option> doesn't contain any value="" property. how would you select it then ?
select/option[#value = "Grass"]
Does Xpath automatically ignore white spaces for the case above? Should it be trimmed?
EDIT:
for selecting multiple options would this suffice ?
select/option[normalize-space(text())="Grass" or normalize-space(text())="Trees"]
To select by text value you can use text() function. And normalize spaces is required, because they are not removed by default. Here is an example:
select/option[normalize-space(text())="Grass"]
#value - value of "value" attribute
#val - value of "val" attribute
normalize-space() - function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space
Well, if whitespace isn't an issue:
/select/option[.='Grass']
I'd need to check re whitespace, though. You could always normalize:
/select/option[normalize-space(.)='Grass']

Resources