首页 > 解决方案 > 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);
    }
    
    
      

标签: javascript

解决方案


对于您的要求,自定义功能可能会解决您的问题。它可能涵盖几乎所有主要场景。我已尽力以最佳可能性更新答案。

请审查它。

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));


推荐阅读