简体   繁体   中英

How to get specific data from input/textarea

I need some help as a beginner in figuring way to pull specific data that was input via textarea.

Lets say I have textarea that user uses for input of certain data, which simply stores input in a variable. Problem I have is how to pull certain parts of that data (stored in variable now) when there is no set pattern on how data is entered.

For example this could be something user would enter:

Name: Random Name

Age: 20

Random Number: 19-12

Phone: 111-111-222

Now, I understand I could use different methods to fetch data when I know its location in variable or when enter pattern is always same but what if it's different. What if someone writes single digit in Age, Phone format is different or random number is different.

Hopefully I was clear enough with my problem. Thanks in advance for your help!

If you are pretty sure that user would always make use of a colon (:) after the field, use this code:

var data = document.querySelector('textarea').value;
var data2 = data.split('\n');

var name = data2[0].split(':')[1];
var age = data2[1].split(':')[1];
var number = data2[2].split(':')[1];
var phone = data2[3].split(':')[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM