Visit The New, Modern Unix Linux Community


Javascript to check field is empty then execute rest of script


 
Thread Tools Search this Thread
Top Forums Web Development Javascript to check field is empty then execute rest of script
# 1  
Javascript to check field is empty then execute rest of script

I have found this bit of code that nearly does what I want.

Basically 3 input fields, I want to copy t2 to t3 as it's typed but only if t1 contains data AND t3 is empty:

Code:
<input type="text" id="t1" /> 
<input type="text" id="t2" />
<input type="text" id="t3" />

 <script> var t2 = document.getElementById('t2'); t2.onkeyup = t12.onchange = function() {     
document.getElementById('t3').value = this.value; }; 
</script>

Thanks
# 2  
Code:
if((document.getElementById('t1').value) && (!document.getElementById('t3').value)) { ... }

You realize of course this will stop copying after the first character, as t3 will no longer be empty.
# 3  
You should do <script type='text/javascript'> fyi.
# 4  
Quote:
Originally Posted by Corona688
Code:
if((document.getElementById('t1').value)  && (!document.getElementById('t3').value)) { ... }

You realize of course this will stop copying after the first character, as t3 will no longer be empty.
Ah, right - that's no good!! so how would I modify the script to copy the contents of t2 to t3 as the user moves away from t2 (ie tabs out of the text box)? I guess I need onblur?

1 other query:
Thinking about the order in which the data will be entered which I have go slightly wrong - t2 will be entered before t1 so I need the script to copy t2 to t3 when t1 is filled?
Thanks for the help - again

Quote:
Originally Posted by Corona688
You should do <script type='text/javascript'> fyi.
Yee, will do this in the future - I was not sure it was required anymore Smilie

---------- Post updated at 08:53 PM ---------- Previous update was at 07:10 AM ----------

Quote:
Originally Posted by Corona688
Code:
if((document.getElementById('t1').value) && (!document.getElementById('t3').value)) { ... }

I am not quiet sure what to do with this, do I put the rest of the code between the curly brackets like this??:

Code:
<script type='text/javascript'>
if((document.getElementById('t1').value) && (!document.getElementById('t3').value))
{
    if((document.getElementById('t1').value))
      {
       var t2 = document.getElementById('t2');
        t2.onblur = function() 
            {
            document.getElementById('t3').value = this.value;
            }
        }
};
</script>

Nothing is copied Smilie

Last edited by barrydocks; 09-24-2014 at 03:21 AM..
# 5  
You can try this code

Code:
if(document.getElementById("question").value.length == 0)
{
    alert("empty")
}


and see if this works for you.

Last edited by Scrutinizer; 06-14-2015 at 06:12 PM.. Reason: CODE tags

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check whether the file is empty or not in shell script (sh)

Hi All, I need to check a file whether it exists and also whether it is empty or not. I have a code for this but it is not working as per my requirement. Can anyone pls suggest me on this. function funcFLSanityCheck { if then echo "${varFLSentFileListPath}/${varFLSentFileName}... (7 Replies)
Discussion started by: Arun1992
7 Replies

2. Shell Programming and Scripting

Nth field should have space and rest commas

Hello I was working on a script where the output of my file is 1234 4567 8973 43214 78965 I need the value in below format of this file.The nth field should have space instead of ,(comma) 1234,4567,8973,43214 78965 I tried the code but not working completely xargs <temp_PP.7250... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

How to add conditional check of whether a process is running before doing rest of script?

Hi, I'm looking at doing a cron job for a script that could take a very short time to complete or a very long time to complete based on variable activity on a server. I don't want to do a weird schedule, but I don't want to be so aggressive that the process attempts to start before another... (14 Replies)
Discussion started by: nbsparks
14 Replies

4. Shell Programming and Scripting

perl script to check read/write/execute permission for 'others'

I want to check access rights permissions not for 'user', not for 'group', but for 'others'. I want to do it by system command in which i want to use 'ls -l' and 'awk' command. I have written the following program : #!/usr/bin/local/perl #include <stdlib.h> system ("ls -l | awk... (1 Reply)
Discussion started by: shubhamsachdeva
1 Replies

5. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

6. Shell Programming and Scripting

Need to check for empty file in C shell script

I am running a C shell script. I have an output file from a previous step and I need to run "something" in the next step to check if the file is empty. If the file is empty, then the job script should finish EOJ. If the file is not empty then the job script should abend. Please help Thanks. (4 Replies)
Discussion started by: jclanc8
4 Replies

7. Shell Programming and Scripting

Awk adding a space between integer and the rest of the field

Hi, I have a problem where some of the records I need to process have the first address field as something like "10Walpole Street" where obviously I want it to be "10 Walpole Street". I know I need to somehow separate out the integer and probably form a new string variable, but I just don't... (5 Replies)
Discussion started by: jonathanm
5 Replies

8. UNIX for Dummies Questions & Answers

Shell Script using Join, empty field help!

Deleted#### (1 Reply)
Discussion started by: tibbyuk
1 Replies

9. Shell Programming and Scripting

How to execute the rest of the code after commenting multiple lines?

Hi, As I have seen in this forum how to comment multiple lines in the script, but it does not work properly for me. It is blocking the code but it does not execute the rest of the codes. This is my code #! /usr/bin/ksh month='date +"m%"' : << Comments Block if || then echo "inc =... (12 Replies)
Discussion started by: Yamini Thoppen
12 Replies

10. Shell Programming and Scripting

Want to execute rest of the script after the file is ready ...

Hi All I have a requirement like, where a file gets generated in a particular dir and once the file is ready and available then I want to execute rest of the script, because untill and unless the file exists and is available there is no use of running rest of the commands in that script. ... (5 Replies)
Discussion started by: csaha
5 Replies

Featured Tech Videos