[英]How to use javascript in django template
I am using javascript for validating a form in django template, but it's not working,我正在使用 javascript 验证 django 模板中的表单,但它不起作用,
the DOM events are not fetching the values.
DOM 事件没有获取值。 even if i give valid input to form the console says empty string
即使我提供有效输入以形成控制台说空字符串
HTML snippet HTML 片段
<form onsubmit="return validate_hotel()" action="confirm_hotel/{{hotel_name}}" method="post">
{% csrf_token %}
<div id="left">
<label>Check in date</label>
<input id="checkin" type="date" autocomplete="off" name="checkin" > <br><br>
<label>Number of guests</label><br>
<select autocomplete="off" name="guests" id="guests">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br><br>
<label>Username</label>
<input id="HotelUsername" type="text" name="username" autocomplete="off">
</div>
<div id="right">
<label>Check out date</label>
<input id="checkout" type="date" autocomplete="off" name="checkout"> <br><br>
<label>Number of rooms</label><br>
<select autocomplete="off">
<option value="1">1</option>
<option value="2">2</option><br>
</select><br><br>
<label> password</label>
<input id="password" type="password" autocomplete="off" name="password" >
</div>
<br><label id="hotel_error_message"> {{error_message}} </label><br>
<button type="submit">Confirm booking</button>
</form>
here is the javascript function i am using to validate things.这是我用来验证事物的 javascript function。 i am using document.getElementById() method to fetch the input values.
我正在使用 document.getElementById() 方法来获取输入值。
function validate_hotel()
{
var hotel_username = document.getElementById("HotelUsername").value;
var checkin = document.getElementById("checkin").value;
var checkout = document.getElementById("checkout").value;
var hotel_password = document.getElementById("password").value;
var date_regx = /^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$/
var username_regx = /^[a-zA-Z0-9_$.@]+$/
var password_regx = /^(?=.*\d).{4,12}$/
var valid = true;
console.log(hotel_username, checkin, checkout, hotel_password);
if (! username_regx.test(hotel_username))
{
valid = false;
}
if (! password_regx.test(hotel_password))
{
valid = false;
}
if (! date_regx.test(checkin))
{
valid = false;
}
if (! date_regx.test(checkout))
{
valid = false;
}
if (!valid)
{
document.getElementById("hotel_error_message").innerHTML = "Invalid inputs";
}
return valid;
};
Javascript code is executed by the browser (client side) and It is not related to backend (django etc...). Javascript 代码由浏览器(客户端)执行,与后端(django 等)无关。 Your code has no problem and works correctly.
您的代码没有问题并且可以正常工作。
To check javascript code I'm using Firefox debugger .要检查 javascript 代码,我正在使用Firefox 调试器。 It is possible to create break points in your javascript code and watch the variables to check if they get value or not.
可以在 javascript 代码中创建断点并观察变量以检查它们是否获得值。
Anyway, the code works correctly check if you have some elements with same name or something like this.无论如何,代码可以正常工作,检查您是否有一些具有相同名称或类似名称的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.