简体   繁体   中英

Javascript error: .split is not a function

I'm trying to separate a full name into first and last name and populate the corresponding fields. However, I keep running into a JavaScript error ".split is not a function"

This is my code

function SplitName (executionContext) {​​​

var formContext = executionContext.getFormContext();    
var FirstName = formContext.getAttribute('firstname');
var LastName = formContext.getAttribute('lastname');
var FullNameField = FullName.getValue();

if (FullNameField =! null){​​​

var SplitField = FullNameField.split(" ",2);
var FirstNameValue = SplitField[0];
var LastNameValue = SplitField[1];
        FirstName.setValue(FirstNameValue);
        LastName.setValue(LastNameValue);
    }​​​

else {​​​
        FirstName.setValue('');
        LastName.setValue('');
    }​​​
}​​​

Any help/suggestion on what am I doing wrong would be greatly appreciated

FullNameField =! null

You're assigning FullNameField to "true" with this. Switch it to:

FullNameField != null

the variable "FullNameField" should be a string before you run "split" method on it.

Hence, try using

FullNameField.toString().split(" ",2);

So somehow this made it work.

function SplitName (executionContext) {​​​

var formContext = executionContext.getFormContext();    
var FirstName = formContext.getAttribute('firstname');
var LastName = formContext.getAttribute('lastname');
var FullNameField = FullName.getValue();

if (Lastname != null){

if (FullNameField != null){​​​

var SplitField = FullNameField.split(" ",2);
var FirstNameValue = SplitField[0];
var LastNameValue = SplitField[1];
        FirstName.setValue(FirstNameValue);
        LastName.setValue(LastNameValue);
    }​​​

else {​​​
        FirstName.setValue('');
        LastName.setValue('');
    }​​​
}
}​​​

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