Warm tip: This article is reproduced from serverfault.com, please click

javascript-如何防止只在Shopify网站上的文本字段中输入空格

(javascript - How to prevent entering space only in a text field on Shopify website)

发布于 2020-11-28 20:33:00

我在网站上输入了文本框,以允许个性化产品。

字段必须包含一些文本,但是某些客户可以通过输入空格并提交表单来解决此问题。这是一个问题,因为个性化设置不能留空,并且必须具有一些文本值,即使它只是点或连字符(例如)。

在文本框只有一个空格作为输入的唯一字符的情况下,如何防止提交表单?

因为是Shopify电子商务商店,所以代码是Liquid

    <div class="personalisation_label">
    <p>Type the personalisation you want below:</p>
    </div>
    <div class="personalisation">
    <input type="text" name="properties[Line 1]" id="Line 1-0-0" maxlength="12" size="12">
    <button type="submit" name="add" id="AddToCart" class="btn {{ btn_class }}{% if section.settings.enable_payment_button %} btn--secondary{% endif %}">
    <span id="AddToCartText">{{ 'products.product.add_to_cart' | t }}</span>
    </button>
    </div>
Questioner
masted75
Viewed
0
Or Assayag 2020-11-30 00:29:00

HTML:

<button type="submit" name="add" id="AddToCart" onclick="return validate();" ... rest of the HTML

JavaScript:

function validate() {
   var text = document.getElementById("Line 1-0-0").value;
   if (!text.replace(/\s/g, '').length) {
       return false;
   }
   return true;
}