[英]Trim function is not working as expected
在我的應用程序中,我需要使用trim函數來發送Id字段。 當我使用一些空格復制電子郵件ID時,它會顯示電子郵件ID。 我不匹配。 如何在java腳本中使用trim()方法。
function validateform() { var email1 = document.getElementById('Email').value; var email = trim(email1); //email check condition email should be in like this if (!(email.endsWith("@hai.com") || email.endsWith("@hello.com"))) { alert("Email Should be in @hi.com or @hello.com"); $("#Email").val(''); document.getElementById("Email").focus(); return false; } } function trim(email2) { return email2.replace(/^\\s+|\\s+$/g, ""); }
我認為你應該使用默認的字符串.trim()函數。
var email = document.getElementById('Email').value.trim();
而已 :)
你必須改變下面的代碼, 檢查這個
function validateform() {
var email1 = document.getElementById('Email').value;
var email = email1.trim();
//email check condition email should be in like this
else if (!(email.endsWith("@hai.com") || email.endsWith("@hello.com")))
{
alert("Email Should be in @hi.com or @hello.com");
$("#Email").val('');
document.getElementById("Email").focus();
return false;
}
}
注意:$ .trim()函數從提供的字符串的開頭和結尾刪除所有換行符,空格(包括不間斷空格)和制表符。 如果這些空格字符出現在字符串的中間,則會保留它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.