我正在使用indexOf來查看一封電子郵件是否包含某個特定文本以外的內容。
例如,我想檢查一封電子郵件是否不包括@符號之后的 "USA",并顯示一個錯誤資訊。
我首先分割文本,并洗掉@符號之前的所有內容:
我首先分割文本,并洗掉@符號之前的所有內容。
var validateemailaddress = regcriteria. email.split('@').pop()。
然后,我檢查文本是否不包括 "USA":
if(validateemailaddress.indexOf('usa'/span>)){
$('#emailError'/span>).show()。
}
上面的檢查似乎有些不對。 它是有效的--我可以輸入一個電子郵件,如果它不包括 "美國",那么錯誤資訊就會出現。
無論如何,當我添加一個額外的檢查時,比如說如果電子郵件不包括 "can",那么無論如何都會顯示錯誤資訊。
如下:
if(validateemailaddress. indexOf('usa') || validateemailaddress.indexOf('can') {
$('#emailError'/span>).show()。
}
如上所述,使用上述方法,無論電子郵件中是否包含文本,都會顯示錯誤資訊。
我想做的是檢查電子郵件是否包括 "USA "或 "Can",如果不包括,則顯示錯誤資訊。
我怎樣才能使其發揮作用呢?
uj5u.com熱心網友回復:
這里有一個簡單的JavaScript函式來檢查一個電子郵件地址是否包含'usa'或'can'。
。function emailValid(email, words) {
//獲取@的位置 [indexOfAt = 3]
let indexOfAt = email.indexOf('@'/span>)。
//獲取@之后的字串 [strAfterAt = domain.usa]
let strAfterAt = email.substring(indexOfAt 1) 。
for (let index in words) {
//檢查該字串是否包含單詞陣列中的一個單詞。
if (strAfterAt.includes(words[index])) {
return true;
}
}
// If the email does not contain any word of the words array; }
//它是一個無效的電子郵件。
return false。
}
let words = ['usa', 'can'];
if (!emailValid('[email protected]'/span>, words)) {
console.log("Invalid Email!") 。
//這里可以顯示錯誤資訊。
} else {
console.log("Valid Email!"/span>)。
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以這樣做,使用包括:
。const validateEmailAdress = (email) => {
const splittedEmail = email.split('@').pop()。
return (splittedEmail.includes('usa') || splittedEmail。 includes('can'))
}
console.log(" Includes usa: ", validateEmailAdress("[email protected]")
console.log(" Includes can: ", validateEmailAdress("[email protected]")
console.log(" Does not includes: ", validateEmailAdress("[email protected]"/span>))
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
有幾種方法可以檢查一個字串是否包含/不包含一個子串。
String.prototype.includes
'String'.includes(searchString); // return true/false
String.prototype.indexOf
// return values from -1 to last postion of string.
'String'.indexOf(searchString)。
//與~結合起來,這可以起到類似于includes()的作用。
//對于長度不超過2^31-1位元組的字串。
///如果沒有找到字串則回傳0,如果找到則回傳-pos。
~'String'.indexOf(searchString)。
在正則運算式的幫助下:
// substring must be escaped to return valid results。
new RegExp(escapedSearchString)。 test('String'); //回傳 true/false if the search string is found。
'String'.match(escapedSearchString); //如果找到則回傳空或陣列。
所以總的來說,你可以使用幾乎所有的方法,如:
if(String'.function(searchString)){
//'String'包括搜索字串。
} else {
// 'String' does not include search String !
或者在indexOf的情況下:
if (String'.indexOf(searchString) > -1) {
// 'String' 包括搜索字串。
} else {
// 'String' does not include search String !
}
// OR
if (~'String'.indexOf(searchString)) {
//'String'包括搜索字串。
} else {
// 'String' does not include search String !
uj5u.com熱心網友回復:
我相信這個正則運算式匹配就是你要找的東西
System.out.println(myString.matches("(.)@(.)usa(.*)");轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/311785.html
標籤:
上一篇:F#-嵌套型別
