ok and cancel buttons on showinputdialog and jlist - jlist

I have an add button that adds entries to a txt file, and a jlist that is populated by the txt file. The jlist is refreshed immediately when an entry is added. The add button utilizes showinputdialog for the user to add an entry and here lays the problem:
1) I understand that when the cancel button is pressed, joptionpane returns null. How do i make it so that it doesn't actually add "null" to the txt file and the jlist when i press cancel?
2) Also, similar problem, if I don't input anything and press the enter button, the program adds an empty entry to the txt file that also show up on the jlist. How do I make it so that if no input is entered and the ok button is pressed, a prompt pops up and tells the user to enter a valid input?
Here's what I've got so far...
#Override
public void actionPerformed(ActionEvent e)
{
String entry = "\n" + JOptionPane.showInputDialog(null, "Enter part number");
if(entry.equals(null))
{
JOptionPane.showMessageDialog(null, "Invalid entry", "Blank entry!", JOptionPane.WARNING_MESSAGE);
}
The if statement here actually doesn't do anything and I don't know why...

In your problem number 2.. also trim your input. you can condition your input like this:
String entry = JOptionPane.showInputDialog(null, "Enter part number");
if(entry == null){ // if the user press cancel;
return;
}
entry = entry.trim();
if(entry.equals("") || entry.isEmpty()){
JOptionPane.showMessageDialog(null,"Please enter a valid input!");
return;
}else{
entry = "\n" + entry;
}

Related

Get the caret position for Blazor text input

I am working on a Blazor textarea input. What I want to achieve is whenever user types "#" character, I am going to popup a small window and they can select something from it. Whatever they select, I will insert that text into the textarea, right after where they typed the "#".
I got this HTML:
<textarea rows="10" class="form-control" id="CSTemplate" #bind="original" #oninput="(e => InputHandler(e.Value))" #onkeypress="#(e => KeyWasPressed(e))"></textarea>
And the codes are:
protected void InputHandler(object value)
{
original = value.ToString();
}
private void KeyWasPressed(KeyboardEventArgs args)
{
if (args.Key == "#")
{
showVariables = true;
}
}
protected void AddVariable(string v)
{
original += v + " ";
showVariables = false;
}
This worked very well. The showVariables boolean is how I control the pop-up window and AddVariable function is how I add the selected text back to the textarea.
However, there is one small problem. If I've already typed certain text and then I go back to any previous position and typed "#", menu will still pop-up no problem, but when user selects the text and the insert is of course only appended to the end of the text. I am having trouble trying to get the exact caret position of when the "#" was so I only append the text right after the "#", not to the end of the input.
Thanks a lot!
I did fast demo app, check it https://github.com/Lupusa87/BlazorDisplayMenuAtCaret
I got it - I was able to use JSInterop to obtain the cursor position $('#CSTemplate').prop("selectionStart") and save the value in a variable. Then use this value later in the AddVariable function.
you can set your condition in InputHandler and when you are checking for the # to see if it's inputed you can also get the length to see that if it's just an # or it has some characters before or after it obviously when the length is 1 and value is # it means there is just an # and if length is more than one then ...

UILabel first text overlaps second text

I am doing signup page's uitextfields validation programmatically. I have given a single label separately under signup button where it shows the error message.
I have given first name text wrong so that label shows "Please enter valid first name" and I have done the same with Last name and now the message "Please enter valid second name" overlaps the first name error message.
How to get two error messages separately using single label?
you can just change the text of the label while showing the Error message
For Eg:
if (**firstNameTextWrong**)
{
lbl.text=#"Please enter valid first name";
}
else if (**secondNameTextWrong**)
{
lbl.text=#"Please enter valid second name";
}
else
{
//do the right part here
[lbl setHidden:YES];
}

Require a textbox to have value in FIllable PDF

I have a fillable PDF file. I would like to require that a TextBox has a value when the user saves the PDF document i.e. the value is not blank.
Here is what I tried:
(1) Setting the "Required" field on the "TextBox".
PROBLEM: That didn't do much except color the textbox red.
(2) I tried to use the following code in the "onBlur" event:
f = getField(event.target.name)
if (f.value.length == 0)
{
f.setFocus()
//Optional Message - Comment out the next line to remove
app.alert("This field is required. Please enter a value.")
}
PROBLEM: If the user never clicks this box there is no problem
(3) I tried to use the "Validation" tab and run a custom JavaScript.
PROBLEM: If you don't click on the box there is no validation so it is perfectly happy to leave the textbox blank if the user forgets to fill it in
OK, I am out of ideas... Anyone?
Since you are using Acrobat JavaScript I assume you use a viewer that supports and executes Acrobat JavaScript. In this situation you can set the document's WillSave action to a custom JavaScript action and perform validation here. I'm not sure if you can cancel the save operation but at least you can display an alert if the validation fails.
UPDATE: This script will loop through all the fields and display and alert if the field value is empty.
for ( var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
var field = this.getField(fieldName);
if (field.value.length == 0)
{
field.setFocus()
app.alert("Field " + fieldName + " is required. Please enter a value.")
}
}
Put the script in document's Will Save action and it will run every time the user saves the form. In Acrobat you set this in Tools > JavaScript > Set Document Actions and select Document Will Save.

Get a Monotouch.Dialog InputElement for a password field

I am trying to get the value from an EntryElement
var password = new EntryElement("Password","Password",null,true);
I have a StyledStringElement with a click event like
new Section()
{
new StyledStringElement ("Login", delegate {
Console.Write (string.Format("Password is {0}",password.Value));
Console.Write (string.Format("Email is {0}",email.Value));
})
When I click on the element I can see the email string value however the password is always null.
What do I need to do differently to get the password value?
make sure you have pressed return on the iPhone keyboard, if not the value will not saved

Dojo OnKeyPress Handler: TextBox value is blank

I have a Dojo form that does not contain a submit button. Instead, I added an onkeypress handler to calls a method when Enter is pressed. The problem I am having is that when I hit enter before blurring off the current field, the _process method thinks that field is empty.
Or in other words: type in field1. hit tab. type in field2. hit enter. field2 is blank unless i click off the field or shift-tab back.
Any ideas?
dojo.connect(dijit.byId("fkrform"),"onKeyPress",function(e) {
if (e.keyCode == dojo.keys.ENTER) {
_process();
}
and the method it calls:
function _process()
{
var field1 = dijit.byId("field1").value;
var field2 = dijit.byId("field2").value;
alert(username);
alert(password);
...do stuff...
}
The fields are of dojoType: dijit.form.TextBox, and the form is: dijit.form.Form
Use dijit.byId('field1').get('value') instead of directly try to access the property "value". In your example you saved the value in the variable field1 and field2 and in the alert you use the variable username and password could be the answer why you don't get anything. But you still should use the get method to get a property instead of directly access the property.
When you press "Enter" your form will submit. So you need to connect to the "onSubmit" event on the form, instead of onkeyPress or onKeyUp.
The first example i created prints the value of the input box on every key someone pressed in the console.
http://jsfiddle.net/a8FHg/
But what you really wanted was hooking into the submit. I modified the example. The new example connects to "onSubmit" and creates an alert box with the text of the user input.
http://jsfiddle.net/a8FHg/1/
For completness if jsfiddle doesn't work some day. You JavaScript should looks like this.
dojo.ready(function(){
var form = dijit.byId('form');
var box = dijit.byId('box');
var submit = function(event) {
dojo.stopEvent(event);
alert("User input was " + box.get('value'));
};
dojo.connect(form, 'onSubmit', submit);
});
Assuming your form in your HTML has the id form and your box have the id box.