Lai
Lai

Reputation: 1

How to not allow just tape all spaces can be submit in using javascript

Textbox should not allow submit just tape all the spaces in it. I try to write but it failed.

Code I used:

<form action="/credential_editor" method="post" name="experience" onsubmit="return check();">
  <form class="form-group">
    <label for="activity_kind">活動類型</label>
    <select class="form-control" id="activity_kind" name="activity_kind" >
    {% for obj in list_credential %}
    <option value="{{obj["activity_kind"]}}">日期:{{obj["Date"]}}: {{obj["ActivityName"]}} - {{obj["EventVenue"]}} ({{obj["Credit"]}} 學分)</option>
    {% endfor %}
    </select>
  <form class="form-group">
    <label for="experience">心得</label>
    <textarea class="form-control" id="experience"  name="experience" rows="5" ></textarea>
    <button type='submit' class="btn btn-primary" value="送出" </button>
</form>
</div>

{% endblock %}

<script type="text/javascript">
                function check(){
                           if(document.experience.experience.value == "")
                           {
                             alert("未輸入心得");
                             document.experience.experience.focus();
                             return false;
                           }
                           return true;
                }
        </script>

Upvotes: 0

Views: 46

Answers (1)

Ibz
Ibz

Reputation: 437

If I'm understanding the problem correctly, you should just be able to use the "trim" method.

Replace this line:

if(document.experience.experience.value == "")

with:

if(document.experience.experience.value.trim() == "")

Upvotes: 1

Related Questions