I have the following JavaScript function. I have two textboxes, two hiddenfields and two labels. and a button. On the click of a button I am calling this function.
On typing value in the textbox, It shows an autoCompleteList. On picking a value from this, it fetches the value from DB and fills the corresponding label. Also, at the same time it stores the corresponding selected value in hiddenfield.
Whenever one changes the value in textbox and without selecting the value from autoPopulated list, It must show the alert.
The function is working fine, If I fill a single textbox, but when I fill both, It goes to the Point A, instead of showing the alert of Point B
function ConfirmBox()
{
var txtBoxValue = document.getElementById("<%= txtFromPartNumber.ClientID %>").value;
var txtBoxValue1 = document.getElementById("<%= txtToPartNumber.ClientID %>").value;
var release = document.getElementById("<%= lblFromPartNumber.ClientID %>").innerText;
var release1 = document.getElementById("<%= lblToPartNumber.ClientID %>").innerText;
var hdnFromValueID = "<%= hdnFromReleaseNumber.ClientID %>";
var hdnToValueID = "<%= hdnToReleaseNumber.ClientID %>";
if ((txtBoxValue && txtBoxValue.trim() != "" && release))
{
if (document.getElementById(hdnFromValueID).value.trim() == txtBoxValue.trim())
{
if ((txtBoxValue1 && txtBoxValue1.trim() != "" && release1))
{
if (document.getElementById(hdnToValueID).value.trim() == txtBoxValue1.trim())
{
return true; //Point A
}
else
{// Point B
//alert("To Part no. has been changed. Please enter it again.");
var msg = document.getElementById("alertMessage");
msg.innerHTML = "To Part no. has been changed. Please enter it again.";
var myExtender = $find("ctl00_ContentPlaceHolder1_ModalPopupExtenderAlert");
myExtender.show();
document.getElementById("<%= txtToPartNumber.ClientID %>").focus();
return false;
}
}
else
{
//window.alert("Please choose a Valid To Part number");
var msg = document.getElementById("alertMessage");
msg.innerHTML = "Please choose a Valid To Part number.";
var myExtender = $find("ctl00_ContentPlaceHolder1_ModalPopupExtenderAlert");
myExtender.show();
return false;
}
}
else
{
//alert("From Part no. has been changed.Please enter it again.");
var msg = document.getElementById("alertMessage");
msg.innerHTML = "From Part no. has been changed.Please enter it again.";
var myExtender = $find("ctl00_ContentPlaceHolder1_ModalPopupExtenderAlert");
myExtender.show();
document.getElementById("<%= txtFromPartNumber.ClientID %>").focus();
return false;
}
}
else
{
//window.alert("Please choose a Valid From Part number");
var msg = document.getElementById("alertMessage");
msg.innerHTML = "Please choose a Valid From Part Number.";
var myExtender = $find("ctl00_ContentPlaceHolder1_ModalPopupExtenderAlert");
myExtender.show();
return false;
}
}
Related
To be honest, I have no idea why the variable is being set to null. The object is set, then once I go through the DisplayPromptAsync, it sets the object to null.
I'm not sure what to try as I've never come across this issue.
Here's a gif of the issue. Once I enter into the field and press submit, an object gets reset.
async void OpenContainerItem()
{
// Pause the timer
blnTimerActive = false;
if (SelectedItem != null)
{
if (SelectedItem.intQuanChecked == SelectedItem.intQuantity)
return;
try
{
int intQuantity = 0;
// Ask for quantity
string result = await Application.Current.MainPage.DisplayPromptAsync("Quantity",
"How many " + SelectedItem.objItem.strItemName + " did you count?",
"Okay", cancel: "Cancel",
placeholder: "Quantity",
keyboard: Keyboard.Numeric);
// Check if it's been cancelled
if (result != null)
{
// check if theres nothing entered
if (result == "")
{
// Why tho?
await Application.Current.MainPage.DisplayAlert("Error", "Please enter a quantity.", "Okay");
}
else
{
intQuantity = int.Parse(result);
if (0 > ((SelectedItem.intQuantity - SelectedItem.intQuanChecked) - intQuantity))
{
Application.Current.MainPage.DisplayAlert("Error", "Thats too many items!", "Okay");
Reload();
blnTimerActive = true;
return;
}
modDatabaseUtilities.ContainerItemsPreCheck(SelectedItem.intContainerItemID, intQuantity, strCurrentUser);
Reload();
}
}
}
catch(Exception ex)
{
Application.Current.MainPage.DisplayAlert("Error", "Couldn't process this change. Please try again.", "Okay");
}
}
#ScottUphus - Is SelectedItem bound to a ListView? (If it is bound to anything, you should add that xaml in your question.) If so, then its a common problem: xamarin sets it back to null when the display layout is refreshed. (I'm not sure the exact "rule" for when it happens. In your case, I suspect the modal interaction causes this.)
This is how I solve such issues:
public MyItemType ValidSelectedItem { get; private set; }
public MyItemType SelectedItem
{
get => _SelectedItem;
set {
... your current setter code here ...
// Remember most recent non-null value.
if (value != null)
ValidSelectedItem = value;
}
}
private MyItemType _SelectedItem;
ValidSelectedItem remembers the non-null item, even if xamarin resets the selection back to null. Use it in code that needs that value.
Is there a difference in using "Okay" or "OK" in DisplayPromptAsync ? try to change it in your code.
this is default Page.DisplayPromptAsync Method:
public System.Threading.Tasks.Task<string> DisplayPromptAsync
(
string title,
string message,
string accept = "OK",
string cancel = "Cancel",
string placeholder = default,
int maxLength = -1,
Xamarin.Forms.Keyboard keyboard = default,
string initialValue = ""
);
public Login ClickGetStatus()
{
//IWebElement btnGetStatus = driver.FindElement(By.XPath("//*[contains(#id,'GetStatus')]"));
do
{
buttonName_GetStatus[0] = "abc";
Thread.Sleep(3000);
bool is_displayed =
wrapper.IsElementDisplayed(
driver.FindElement(By.XPath("//*[contains(#id,'GetStatus')]")));
//bool IsElementDisplayed = driver.FindElement(By.XPath("//*[contains(#id,'GetStatus')]")).Displayed;
if (is_displayed)
{
//wrapper.Click(btnExecute);
string getnameofbutton1 =
driver.FindElement(
By.XPath("//*[contains(#id,'GetStatus')]")).GetAttribute("id");
Console.WriteLine("Name of the button is : " + getnameofbutton1);
buttonName_GetStatus = getnameofbutton1.Split('_');
driver.FindElement(
By.XPath("//*[contains(#id,'GetStatus')]")).Click();
}
else
{
Console.WriteLine("Element is not displayed");
}
}
while (buttonName_GetStatus[0] == "GetStatus");
return this;
}
Below is the Logic for the above code
Checks for the button called Get Status
if it finds the button Get Status then clicks on it
i have used contains in the xpath as the element id for that button changes dynamically.
The above code runs fine and clicks on the Get Status button but doesn't come out from the loop when the name of the Get Status button changes to View Result and still searches for Get Status button
If the expected ID of the button after being updated is "ViewResult", then you can update your condition to use that.
while (buttonName_GetStatus[0] != "ViewResult");
This will keep looping round whilst the button does not equal "ViewResult".
Is this the behavior you're trying to achieve?
public Login ClickGetStatus()
{
//IWebElement btnGetStatus = driver.FindElement(By.XPath("//*
[contains(#id,'GetStatus')]"));
do
{
buttonName_GetStatus[0] = "abc";
Thread.Sleep(3000);
var elements = driver.FindElements(By.XPath("//*[contains(#id,'GetStatus')]"));
var is_displayed = elements.Count > 0;
//bool IsElementDisplayed = driver.FindElement(By.XPath("//*[contains(#id,'GetStatus')]")).Displayed;
if (is_displayed)
{
//wrapper.Click(btnExecute);
string getnameofbutton1 =
driver.FindElement(
By.XPath("//*[contains(#id,'GetStatus')]")).GetAttribute("id");
Console.WriteLine("Name of the button is : " + getnameofbutton1);
buttonName_GetStatus = getnameofbutton1.Split('_');
driver.FindElement(
By.XPath("//*[contains(#id,'GetStatus')]")).Click();
}
else
{
Console.WriteLine("Element is not displayed");
}
}
while (buttonName_GetStatus[0] != "ViewResult");
return this;
}
I think problem might be here, especially when you check if isDisplayed == true then this line buttonName_GetStatus = getnameofbutton1.Split('_'); overrides array so that infinitive loop appeared.
Hello please help me out in writing switch statement for the below else if statement
if(ClientAddressTextBox.Text == "")
{
MessageBox.Show("Please Enter Client Address");
this.ActiveControl = ClientAddressTextBox;
}
else if (InnerpathTextBox.Text == "")
{
MessageBox.Show("Please Enter Internal Path");
this.ActiveControl = InnerpathTextBox;
}
else if (InspectorIDTextBox.Text == "")
{
MessageBox.Show("Please Enter Inspector ID");
this.ActiveControl = InspectorIDTextBox;
}
else if (SerialNumberTextBox.Text == "")
{
MessageBox.Show("Please Enter Serial Number");
this.ActiveControl = SerialNumberTextBox;
}
You could do something like this:
var controls = new []
{
new {Ctrl = InnerpathTextBox, Error = "Please Enter Client Address"},
new {Ctrl = ClientAddressTextBox, Error = "Please Enter Internal Path"},
new {Ctrl = InspectorIDTextBox, Error = "Please Enter Inspector ID"},
new {Ctrl = SerialNumberTextBox, Error = "Please Enter Serial Number"}
};
var firstToFailValidation = controls.FirstOrDefault(item => item.Ctrl.Text == "");
if (firstToFailValidation != null)
{
MessageBox.Show(firstToFailValidation.Error);
this.ActiveControl = firstToFailValidation.Ctrl;
}
You might want to check for nulls though. This code assumes that none of the controls or the .Text properties are null.
I'd approach this as follows:
var validationMessages = new[]{new{Control = InnerpathTextBox,
Message = "Please Enter Internal Path"},
new{Control = InspectorIDTextBox,
Message = "Please Enter Inspector Id"},
//etc
};
foreach(var vm in validationMessages)
{
if(string.IsNullOrWhiteSpace(vm.Control.Text))
{
MessageBox.Show(vm.Message);
this.ActiveControl = vm.Control;
break;
}
}
You can use Something like this:
private bool IfTextBoxEmpty(string text, string message, Control control)
{
if (text == "")
{
MessageBox.Show(message);
this.ActiveControl = control;
return true;
}
return false;
}
And the Usage:
if (!IfTextBoxEmpty(ClientAddressTextBox.Text, "Please Enter Client Address", ClientAddressTextBox)
{
//if not - Do Something
}
This is a pseudo code for a switch statement.
string statement;
switch(statement)
{
case (ClientAddressTextBox.Text == ""):
MessageBox.Show("Please Enter Client Address");
this.ActiveControl = ClientAddressTextBox;
break;
case (InnerpathTextBox.Text == ""):
...
}
and so on.
I'm trying to run a validation on the textboxes of an asp page using javascript, but my second javascript function doesn't work as expected. I run the validation on the onblur event of each textbox, so I do:
<asp:TextBox ID="txtRegApellidoPat" runat="server" onblur="validaFormato(this);"> </asp:TextBox></td><td><asp:Label ID="valtxtRegApellidoPat" class="validaCad" runat="server">Únicamente texto</asp:Label>
Then on the textboxes that require integrers I do:
<asp:TextBox ID="txtRegEdad" runat="server" onblur="validaFormatoNum(this);"></asp:TextBox></td><td><asp:Label ID="Label1" class="validaNum" runat="server">* Solo números</asp:Label>
However this last function (validaNum) isn't working, and seems to return false always on the regularexpression.test method that I invoke.
My javascript code is like so:
<script type="text/javascript">
var formato = document.getElementById('formReg');
var textos = new Array("txtRegNombre", "txtRegApellidoPat", "txtRegApellidoMat", "txtRegEdo");
var numeros = new Array("txtRegEdad", "txtRegTel", "txtRegCP");
var pattern = /([A-Z]|[ÁÉÍÓÚáéíóúÄËÏÖÜäëïöü])+[^\d+]/g;
var pattern2 = /^\d$/g;
var invalidos1=document.getElementsByClassName("validaCad");
var invalidos2=document.getElementsByClassName("validaNum");
function validaFormato(obj) {
var valido = false;
if (!pattern.test(obj.value)&&!obj.value=="") {
invalidos1[0].style.display = "inline";
invalidos1[1].style.display = "inline";
invalidos1[2].style.display = "inline";
invalidos1[3].style.display = "inline";
return valido;
}
else {
invalidos1[0].style.display = "none";
invalidos1[1].style.display = "none";
invalidos1[2].style.display = "none";
invalidos1[3].style.display = "none";
valido = true;
}
return valido;
}
function validaFormatoNum(obj) {
var valido = false;
if (!pattern2.test(obj.value) && !obj.value == "") {
invalidos2[0].style.display = "inline";
invalidos2[1].style.display = "inline";
invalidos2[2].style.display = "inline";
return valido;
}
else {
invalidos2[0].style.display = "none";
invalidos2[1].style.display = "none";
invalidos2[2].style.display = "none";
valido = true;
}
return valido;
}
</script>
I'd really appreciate a hint on this as I can't realize what's the problem. The thing is that the number fields behave erratically, when I write a number on them the first time, the associated label doesn't show, which is correct behavior, but then when I tab through the form and tab out of the number field again, the label shows, as if the value was not a number, then on the following tab, the label doesn't show, then it shows, then it doesn't, etcetera.
I'm really at a loss when it comes to regular expressions so I don't know if the problem is with my second RegExp, but it's simply asking to have a number at the beginning and end of the string, isn't it?
I guess you have to change
var pattern = /([A-Z]|[ÁÉÍÓÚáéíóúÄËÏÖÜäëïöü])+[^\d+]/g;
to (move the + out of the character class):
var pattern = /([A-Z]|[ÁÉÍÓÚáéíóúÄËÏÖÜäëïöü])+[^\d]+/g;
that could be rewritten:
var pattern = /[A-ZÁÉÍÓÚáéíóúÄËÏÖÜäëïöü]+\D+/g;
i want to display error message without alert box by using javascript.
i have tried with code.
function validatetextbox() {
var txtuname = document.getElementById('<%=txt_uname.ClientID %>').value;
if(txtuname=="") {
document.getElementById("text_uname").innerHTML= "Enter Username"; } }
but it is not working, please give some suggestions
You need to use document.getElementById('<%=txt_uname.ClientID %>') instead of document.getElementById("text_uname")
Complete function
function validatetextbox() {
var txtuname = document.getElementById('<%=txt_uname.ClientID %>').value;
if (txtuname == "") {
document.getElementById('<%=txt_uname.ClientID %>').innerHTML = "Enter Username";
}
}
try to cache dom, and set innerHTML
function validatetextbox() {
var inputEl = document.getElementById('<%=txt_uname.ClientID %>'),
txtuname = inputEl.value;
if ( txtuname == "" ) {
inputEl.innerHTML = "Enter Username";
}
}
It seems like you need to show alert in txt_uname .. then you should do like this:
function test(){
var txtuname =document.getElementById('<%=txt_uname.ClientID %>').value;
if(txtuname == "") {
document.getElementById('<%=txt_uname.ClientID %>').value= "Enter Username";
}
}
try this
function test()
{
var row = lnk.parentNode.parentNode;
var txtuname= row.cells[your cell number].childNodes[your node number].innerHTML;
if(txtuname=="")
{
txtuname="enter username";
}
}