javascript regex
regex這東西不常用, 每次用都要google老半天.
這次是因為輸入的文字會變成url slug, 所以必須使javascript做出如果是非文字都不能輸入(包括標點或空格)
<h1>Create a Thing</h1>
<form role="form" action="" method="post" enctype="multipart/form-data">
<input type='hidden' name='csrfmiddlewaretoken' value='CiTMjJgFuTMRatFsQRBUT6z8JuyRhe2g' />
<p><label for="id_name">Name:</label> <input id="id_name" maxlength="255" name="name" type="text" /></p>
<p><label for="id_image">Image:</label> <input id="id_image" name="image" type="file" /></p>
<p><label for="id_description">Description:</label> <textarea cols="40" id="id_description" name="description" rows="10">
</textarea></p>
<input type="submit" value="Submit" />
</form>
<script>
function fixInput(event) {
var rx = /[!-#%-*,-/\:;$?=?˙ˋˇˊ=·、‘’@\[-\]_\{\} ]+/gi;
event.target.value = event.target.value.replace(rx, '');
}
document.getElementById('id_name').addEventListener('keyup', fixInput);
document.getElementById('id_name').addEventListener('change', fixInput);
</script>
以上是要判斷input id="id_name" 裡只能有文字(不能有標點和空格及其它符號)
參考
這次是因為輸入的文字會變成url slug, 所以必須使javascript做出如果是非文字都不能輸入(包括標點或空格)
<h1>Create a Thing</h1>
<form role="form" action="" method="post" enctype="multipart/form-data">
<input type='hidden' name='csrfmiddlewaretoken' value='CiTMjJgFuTMRatFsQRBUT6z8JuyRhe2g' />
<p><label for="id_name">Name:</label> <input id="id_name" maxlength="255" name="name" type="text" /></p>
<p><label for="id_image">Image:</label> <input id="id_image" name="image" type="file" /></p>
<p><label for="id_description">Description:</label> <textarea cols="40" id="id_description" name="description" rows="10">
</textarea></p>
<input type="submit" value="Submit" />
</form>
<script>
function fixInput(event) {
var rx = /[!-#%-*,-/\:;$?=?˙ˋˇˊ=·、‘’@\[-\]_\{\} ]+/gi;
event.target.value = event.target.value.replace(rx, '');
}
document.getElementById('id_name').addEventListener('keyup', fixInput);
document.getElementById('id_name').addEventListener('change', fixInput);
</script>
以上是要判斷input id="id_name" 裡只能有文字(不能有標點和空格及其它符號)
參考
留言
張貼留言