Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-25-2012, 08:12 PM   PM User | #1
jquerynewbie
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jquerynewbie is an unknown quantity at this point
Parsing Pasted Text with Jquery (no server side processing)

I need a solution to do the following

1.User pastes some text into a textarea
2.User clicks a button "Autofill"
3.On press of the button, the various lines of the pasted text are put into the appropriatly named form fields.

Here is an example of how the pasted text is formatted:

Code:
First Name Jimberly  
Last Name Hagg  
Postcode Test
I've started work on this project here:
http://jsfiddle.net/hc8kL/1/

Any help/guidance (including coding examples) would be appreciated!

(By the way my jquery to disable the "Continue" button is buggy - it enables the button if you enter some text into one of the forms, but then if you delete it, this isn't noticed by the function and it still keeps the "Continue" button enabled)
jquerynewbie is offline   Reply With Quote
Old 11-26-2012, 05:07 AM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
here is a really simple example. it assumes your textarea has an id of "textareaid" and your auto fill button has an id of "autofillid"
- side note you should really have a valid form element. having valid html can only help you.


Code:
function getuserdata(){
var $ta= $("#textareaid");
var $data= $ta.val().split('\n');
var $first = $data[0].replace('First Name');
var $last = $data[1].replace('Last Name');
var $post = $data[2].replace('Postcode');
$('#f1').val($.trim($first));
$('#f2').val($.trim($last));
$('#f3').val($.trim($post));
}

});
$("#autofillID").click(function(){
getuserdata();
});
Also, if you wanted to, you can do away with the autofill button and jsut fill the fields automatically upon the paste event like so:


Code:
$("#textareaid").bind('paste',function(){
getuserdata();
});
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

Last edited by DanInMa; 11-26-2012 at 05:25 AM..
DanInMa is offline   Reply With Quote
Old 11-26-2012, 05:35 AM   PM User | #3
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
corrections: http://jsfiddle.net/hc8kL/29/


Code:
function getuserdata(){
    setTimeout(function() {
var $ta= $("#textareaid");
var $data= $ta.val().split('\n');
var $first = $data[0].replace('First Name',"");
var $last = $data[1].replace('Last Name',"");
var $post = $data[2].replace('Postcode',"");
$('#f1').val($.trim($first));
$('#f2').val($.trim($last));
$('#f3').val($.trim($post));
        $ta.val('')
       }, 150);
}
$(document).ready(function(){
$("#autofillid").parent().click(function(e){
    e.preventDefault();
getuserdata();
});
$("#textareaid").live('paste',function(){
    getuserdata();
});
});
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 11-27-2012, 11:52 PM   PM User | #4
jquerynewbie
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jquerynewbie is an unknown quantity at this point
Thanks for that guys, it's helpful. I checked it out but seems my button "Continue" is broken with that code added? Also the code to enable the "Continue" doesn't really work lol (see my initial post).
jquerynewbie is offline   Reply With Quote
Old 11-28-2012, 07:31 PM   PM User | #5
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
you just want the first name field to have something in it before you will allow continue right? you dont care about last name or post code?
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 12-03-2012, 05:54 PM   PM User | #6
jquerynewbie
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jquerynewbie is an unknown quantity at this point
Really I want all form fields to be required, there will be other fields but I haven't added them all in yet. Obviously you can either fill in the form fields manually or by pasting them in and using the autofill function (see above)....there's a lot more functionality I need to get into this, but at least it's a start..!

I did previously have this function running:
Code:
function checktext() {
   
if($("#f1").val().length > 0 && $("#f2").val().length > 0 && $("#f3").val().length > 0)  {
    $('#stage1').removeAttr('disabled');}
    else {document.getElementById("stage1").disabled = true;}
}
See:
http://jsfiddle.net/2p5Eg/88/


with the form field attribute
Code:
onkeyup="checktext()"
is there a better way of doing this?

Last edited by jquerynewbie; 12-03-2012 at 06:32 PM..
jquerynewbie is offline   Reply With Quote
Old 12-04-2012, 12:41 AM   PM User | #7
jquerynewbie
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jquerynewbie is an unknown quantity at this point
@DatinMa. Yes I do care about all form fields, I made a function here, but is this the best way to do something like this?


http://jsfiddle.net/hc8kL/35/

Code:
function checktext() {
   
if($("#f1").val().length > 0 && $("#f2").val().length > 0 && $("#f3").val().length > 0)  {
    $('#stage1').removeAttr('disabled');}
    else {document.getElementById("stage1").disabled = true;}
}
jquerynewbie is offline   Reply With Quote
Old 12-04-2012, 01:54 AM   PM User | #8
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
http://jsfiddle.net/hc8kL/38/
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 12-04-2012, 08:14 AM   PM User | #9
jquerynewbie
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
jquerynewbie is an unknown quantity at this point
What changed in that fiddle?
The radio boxes aren't working so I fixed them.

The functionality of the form fields requiring text still doesn't work 100%, if you write something in all 3 fields, it enables the button and then delete the text from one of them, it still keeps the continue button enabled.

But the button code doesn't work now. If you click the button when it's enabled, it doesn't hide/show the elements.
Code:
<button id='stage1'            onclick="hide('foo');show('bar');update();">Continue</button>
Sorry if I'm not being clear. I'll have a closer look tonight.
jquerynewbie is offline   Reply With Quote
Reply

Bookmarks

Tags
jquery, parse, paste, textbox, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:43 AM.


Advertisement
Log in to turn off these ads.