Reputation: 23
I created a form that has a textbox where I expect the end-user to end an email address (and more than one, where applicable).
I have a piece of code that works quite well for a single e-mail:
if (event.value && !eMailValidate(event.value)) {
app.alert("Incorrect Address Format");
event.rc = false;
}
However this does not take more than one e-mail, separated by a semicolon, into consideration. Can anybody shed some light on how to evolve this into a "multiple e-mail validator"?
Thanks, folks.
Tony
Upvotes: 0
Views: 1040
Reputation: 3177
You can parse event.value into a string array and then validate each string in the array using your eMailValidate function. In this case, you want to make sure event.value is not null or empty before attempting to parse it, otherwise your code will break. Here's an example (using your code):
// Make sure event.value is not null or empty
if(event.value == null | event.value == ""){
app.alert("Incorrect Address Format");
event.rc = false;
}
// Split semicolon deliminated values into array
var emailArray = event.value.split(';');
// Get the array length
var len = emailArray.length;
// Loop through and validate
for (i = 0; i < len; i++){
if(!eMailValidate(emailArray[i].trim())){ // Trim removes leading-trailing spaces
app.alert("Incorrect Address Format");
event.rc = false;
}
}
Here's a jsfiddle that has a working example of the code.
[Note] - the issue in the original example was the 's' in Split was capitalized and the $.each loop is specific to jQuery, not javascript.
Hope this is helpful :)
Upvotes: 1