简体   繁体   English

使用react和es6清理电子邮件输入

[英]sanitizing email input using react and es6

I'm trying to go through my state and pull out the contents of recEmail, cc and bcc. 我试图通过我的状态,并取出recEmail,cc和bcc的内容。 Inside each, you can have multiple emails seperated by a ,. 在每个内部,您可以用,分隔多个电子邮件。 For example 例如

recEmail could have: email1@email.com, email2@email.com recEmail可以有:email1@email.com,email2@email.com
cc could have: email3@email.com, email4@email.com 抄送可能包含:email3@email.com,email4@email.com

I'm using lodash to loop through the "this.state" object and push the emails into an array, but then i'd have to loop through this new array to seperate out the emails into 1 email per array position (so i can then run a regex and see if it's a valid email or not). 我正在使用lodash遍历“ this.state”对象并将电子邮件推送到一个数组中,但是然后我必须遍历这个新数组以将电子邮件分成每个数组位置1封电子邮件(因此我可以然后运行一个正则表达式,看看它是否是有效的电子邮件)。

This method seems very messy and I can't help but think there's a better way to go about this. 这种方法看起来很杂乱,我不禁想到有一个更好的方法可以解决此问题。 Here's what I have so far. 到目前为止,这就是我所拥有的。

 this.state = {recEmail: '', cc: '', bcc: '', subject: '', bodyText: '', testInput: false}; stateCleaner(emails){ var emailstoClean = []; _.forOwn(emails, (value, key) => { console.log('these are the keys: ', key); console.log('these are the values: ', value); if(key !== 'testInput'){ emailstoClean.push(value.split(', ')) } }) console.log('final emailstoClean: ', emailstoClean); return emailstoClean } 

This gives me two emails in index zero of the emailstoClean array, and two emails in position 1 (I entered two in the recEmail, and two in the cc field). 这给了我两封电子邮件,它们在emailstoClean数组的索引零中,并且有两封电子邮件在位置1中(我在recEmail中输入了两封,在cc字段中输入了两封)。

Is there a way I can simplify this further to just get one email in each array index? 有没有一种方法可以进一步简化此过程,使每个数组索引仅收到一封电子邮件? Also maybe it's slightly related but including a regex for how to check if it's a valid email would be great too. 另外也许它有点相关,但是包含一个正则表达式来检查它是否是一个有效的电子邮件也很好。 If there's any other details you all need just let me know. 如果还有其他详细信息,请告诉我。 I appreciate everyone's assistance. 感谢大家的帮助。

Few things: emails are surprisingly complicated - a quick google will confirm that regex is not the best way to validate them. 几件事:电子邮件异常复杂-快速的Google会确认正则表达式不是验证邮件的最佳方法。

For your emails to array, you don't need lodash Tke your email strings directly from your state properties and call split(',') on each of them. 为了使电子邮件能够排列,您不需要直接从状态属性中输入电子邮件字符串,也不必在每个属性上调用split(',')。 This will give you an array of emails. 这会给您一系列电子邮件。 Do this for all the email items in your state object and then call concat on the arrays or use the new spread operator to chuck them all in a single array with unique indexes. 对状态对象中的所有电子邮件项目执行此操作,然后在数组上调用concat或使用新的价差运算符将它们全部夹在具有唯一索引的单个数组中。

Then you can use map, filter or some to process each email address in the array. 然后,您可以使用地图,过滤器或一些过滤器来处理阵列中的每个电子邮件地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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