javascript - Javascript 需要防止输入空格
问题描述
以下 Javascript 附加到字段表单(更改时),它应该确保如果用户单击按钮,则“站外”将填充到 activity_type。如果没有,则会出现“95 Modifier”。此外,此表单有一个字段,我已选中“必填”,但发生的情况是用户能够为活动类型输入空白。有没有办法在这个 javascript 中不允许输入空白?
if (getFormElement('activity_type_id').toUpperCase()=='EE641670-8BE3-49FD-8914-030740D9DE72'
&& getFormElement('actual_location').toUpperCase()!='5E74C25C-6363-46BE-B030-16216B364F5A')
{
setFormElement('is_off_site',true);
} else
{
setFormElement('is_off_site',false);
}
{
setFormElement('is_off_site',false);
}
解决方案
对于您的要求,自定义功能可能会解决您的问题。它可能涵盖几乎所有主要场景。我已尽力以最佳可能性更新答案。
请审查它。
function isEmpty(inputValue) {
if(typeof inputValue === 'string' && (inputValue === '0' || inputValue === 'false' || inputValue === '[]' || inputValue === '{}' || inputValue === '')){
return true;
}else if(Array.isArray(inputValue) === true){
return inputValue.length === 0 ? true : false;
}else if(Array.isArray(inputValue) === false && (typeof inputValue === 'object' && inputValue !== null) && typeof inputValue !== 'undefined' && typeof inputValue !== null){
return Object.keys(inputValue).length === 0 ? true : false;
}else if(typeof inputValue === 'undefined'){
return true;
}else if(inputValue === null){
return true;
}else if(typeof inputValue === 'number' && inputValue === 0){
return true;
}else if(inputValue === false){
return true;
}else if(inputValue.length > 0 && inputValue.trim().length === 0){
return true;
}
return false;
}
console.log("isEmpty(' '): ",isEmpty(' '));
console.log("isEmpty(''): ",isEmpty(''));
console.log("isEmpty([]): ",isEmpty([]));
console.log("isEmpty({}): ",isEmpty({}));
console.log("isEmpty(): ",isEmpty());
const nullValue = null;
console.log("isEmpty(null): ",isEmpty(nullValue));
console.log("isEmpty(0): ",isEmpty(0));
console.log("isEmpty(false): ",isEmpty(false));
console.log("isEmpty('0'): ",isEmpty('0'));
console.log("isEmpty('false'): ",isEmpty('false'));
console.log("isEmpty('[]'): ",isEmpty('[]'));
console.log("isEmpty('{}') ",isEmpty('{}'));
console.log("isEmpty(''): ",isEmpty(''));
console.log("isEmpty('0.0'): ",isEmpty(0.0));
推荐阅读
- java - 调用 powermockito 测试方法时出现 NullPointerException
- javascript - 如何在输入文本框中使用 vanilla javascript 防止未来日期
- python - 将一列除以另一列返回 NAN
- angular - 成功登录后路由器不会重定向用户
- python - PyQt5/QSql:应用程序找不到 postgres 表
- r - 使用 gt 或 gtsummary 在 R-markdown 的漂亮整洁表中显示变量类
- python - 如何使二值图像更清晰以提高 tesseract image_to_text 的准确性?
- javascript - 链接标签不使用反应进入 Flexbox
- node.js - (节点:21484)UnhandledPromiseRejectionWarning:TypeError:manager.push 不是函数
- reactjs - React dnd - 棋盘教程示例问题