How can i typetext of a number input in testcafe - testcafe

when i try to typetext as string on an input with type=number the browser display different value
var items = ["15300", "06500", "15400","24500","30580","77104","92730"];
const zipcode =items[Math.floor(Math.random()*items.length)];
console.log(zipcode);
return String(zipcode)
}
.typeText(AddressesLocators.txt.txtZipcode,await Utils.getRandomZipcode(),{ replace: Boolean })

This is because when setting the "type" attribute of an HTML input element to "number", the browser expects the entered value to be a valid number and therefore may attempt to convert the entered value to a number.
If you enter a string like "15300" in an input with type="number", the browser will try to convert the string to a number, in which case the resulting value will be 15300. However, if you enter a string like "06500 ", the browser will interpret the value as an octal number and then convert it to decimal, which will result in 3328 instead of 6500.
To avoid this behavior, you can use an input with type="text" instead of type="number" if you want the entered value to be treated as a string. Or, if you need to use type="number", make sure the value you enter is a valid number and does not start with a leading zero, such as "6500" instead of "06500".
I hope this helps.

Related

Regular expression restrictions

I am new to regular expressions. I have a text field and I need to limit the input to numbers and one comma. I know how to limit to numbers. But I have another condition. If the user enters a comma, they must enter another number after that. There is no restriction on the number of digits. There can only be one comma. The following are all valid inputs:
123456
4567,8907
but I can't allow 4567,.
I have this pattern, pattern="[0-9]+([,][0-9]+)?"
but it's not working.
The following regex allows any number of digits (including none), followed optionally by a single comma, followed by any number of digits. This pattern must span from beginning to end using the ^ and $ symbols, so no other characters are allowed:
^[0-9]*,?[0-9]+$
Try here:
let inp = document.getElementsByTagName('input')[0];
let p = document.getElementsByTagName('p')[0];
inp.addEventListener('input', () => {
let result = inp.value.match(`^[0-9]*,?[0-9]+$`);
p.innerHTML = `Is "${inp.value}" allowed? ${result ? 'YES' : 'NO'}`;
});
body { font-family: monospace; }
<input type="text" placeholder="try a value here"/>
<p></p>

Cant get value from input field

I have an input field with datetime-local type, on editing the form, I cant get the value (which is datetime). In UI, it has the ----- --:--, but when inspecting the code, VALUE parameter has data
Ive tried to document.getElementById().value, $().val(), $().text(), but all of them return "" (empty)
<input type="datetime-local" id="date-from" required="required" value="2019-08-20 05:11:00 UTC" name="pass_request[date_from]">
document.getElementById('date-from').value === ""
$('#date-from').val() === ""
$('#date-from').text() === ""
I value to return, but not emptiness
You should format your value according to RFC 3339 as written here - https://www.w3.org/TR/html-markup/datatypes.html#form.data.datetime-local
The following parts, in exactly the following order:
A date.
The literal string "T".
A time.
Example:
1985-04-12T23:20:50.52
1996-12-19T16:39:57
So try
<input type="datetime-local" id="date-from" name="pass_request[date_from]" value="2015-02-15T03:35:00">

Cannot parse JSON String data to Integer

Suppose my JSON is like this.
// {
// "count": 32,
// "weight": 1.13,
// "name": "grape",
// "isFruit": true
// "currentPrice" : "30.00"
// }
If I read my JSON like this,
String current = json.getString("currentPrice");
the current variable will have value as "30.00". Is there any way that I can parse this as an Integer? I tried doing Integer.parseInt but It is giving an error like Number format exception for input string "30.00".
I tried removing quotes by applying regex but didn't work.
You need to use
parseInt('current')
parseInt(num); // default way (no radix)
parseInt(num, 10); // parseInt with radix (decimal)
parseFloat(num) // floating point
Number(num); // Number constructor
to get current
You want parseFloat(). 30.00 isn't an integer, even though it's numerically EQUAL to the integer 30.
If you want it as an integer, you can use Math.floor() to convert it to one, or you can use parseInt() to get the integer portion, but if you really want the whole value (if it might not always be whole), parse it as a float.

MVC: Pass ViewData object with type 'double' to number input field

I’m trying to pass a ViewData object of type double to an input field of type number with 1 decimal place. But the input field keeps empty. What am I doing wrong?
<p>
Debiet factor: <input name="FlowFact" type="number" value="#ViewData["FlowFact"]" step="0.1" autocomplete="off">
#String.Format("{0:N1} m³/h", #ViewData["FlowFact"]) <!--Result = 3,1 m³/h-->
</p>
The issue you're facing is what the ViewData["FlowFact"] is returning, which according to your #String.Format("{0:N1} m³/h", #ViewData["FlowFact"]) <!--Result = 3,1 m³/h--> it's 3,1.
That is not recognised as a number so it won't work. Either return a whole number or a decimal number. So instead of a 3,1, return a 3.1 or just a 3.
Otherwise change the input type to accept what you are passing.
I found the solution thanks to jamiedanq.
In my controller I was writing a double value to the #ViewData["FlowFact"] object. But in my view it was returning a value 3,0999 , which is not a correct value for the input type 'Number' because of the ",".
Instead of passing a double value to the object I've changed it to pass a string value and replace the "," with a ".":
Controller:
#ViewData["FlowFact"] = #String.Format("{0:N1}", MyDoubleValue).Replace(",",".");
View:
<p>
Debiet factor: <input name="FlowFact" type="number" value="#ViewData["FlowFact"]" step="0.1" autocomplete="off">
</p>

Setting an MVC TextBox to Initial Value in Razor

I'm new to ASP.NET MVC but I haven't been able to find an explanation for this.
My questions is regarding the difference in the value attribute in the generated HTML when I use #HtmlTextBox() vs. #HtmlTextBoxFor().
I can set the initial value for an <input> using #Html.TextBox() like this:
#Html.TextBox("Phone", "Initial Value", htmlAttributes: new { id = "txt_phone" })
The generated HTML is just what you'd expect:
<input id="txt_phone" name="Phone" type="text" value="Initial Value" />
Please notice the generated value attribute above.
Using #HtmlTextBoxFor() is a different. Please note that I have a very simple model in my view. It has a Phone property which is a String.
Here's an attempt at setting an initial value using #Html.TextBoxFor():
#Html.TextBoxFor(x => x.Phone, htmlAttributes: new { id = "txt_phone", value="Initial Value" })
The generated HTML, however, does not reflect the value attribute:
<input id="txt_phone" name="Phone" type="text" value="" />
My first question is, "why did the generated HTML not reflect the 'Initial Value' text in my value attribute?"
As many of you know, the "right way" to set the initial value with #HtmlTextBoxFor() is like this:
#Html.TextBoxFor(x => x.Phone, htmlAttributes: new { id = "txt_phone", Value = "Initial Value" })
But look at the generated HTML:
<input Value="Initial Value" id="txt_phone" name="Phone" type="text" value="" />
As you can see it generates a Value attribute (with a capital V) yet it still generates a value attribute with a lowercase v and an empty string!.
My second question then, is, why does #Html.TextBoxFor() require a captial V in Value yet still generate a lower-case v, value with an empty string?
Thanks
The answer to "why?" is because this is not the way you're supposed to pass a value. The HTML helpers use a bit of fairly complex logic to determine what the value of a field should be, and because it varies based on a number of different circumstances, your attempt at adding a manual value are largely ignored.
The first place Razor looks for a value is in ModelState, which is composed of the data from Request, ViewData and ViewBag. Then, it looks on the view's Model. Finally, it will fallback to the "default" value, which really only applies with the non-For helpers, where you can specify the value to default to. The reason you can't do the same with the For helpers is because they are bound to an actual property, and therefore, take that property's value, even if it's just the default of null or 0 or something.
Long and short, if you want to bind to a property and have it default to a specific value, then that value needs to be the default for the property. For example:
private string phone;
public string Phone
{
get { return phone ?? "Initial Value"; }
set { phone = value; }
}
Now, the property itself will always return "Initial Value" if it's previously unset, so your form field will as well.