I have some form validation on my website.
If the account code field contains "XXX" and the reference field is blank, I want an alert to come up for the user to populate the reference field.
I have read that indexOf is the function I need, but the code below does not appear to work. Any ideas?
<SCRIPT>
if (form.account.value.indexOf("XXX") != -1 & form.reference.value == "") {
alert("Please Enter Reference Number");
form.reference.focus( );
return false;
}
</script>
Can you try:
if (form.account.value.indexOf("XXX") != -1 && form.reference.value == "") {
alert("Please Enter Reference Number");
form.reference.focus( );
return false;
}
This corrects the AND clause, which uses && not &.
Related
I am trying to validate my email and Title input field but it still aloows me to submit the page. Where is the error in the code please?
if(email == "") {
printError("emailErr", "Please enter your email address");
} else {
var regex = /^\S+#\S+\.\S+$/;
if(regex.test(email) === false) {
printError("emailErr", "Please enter a valid email address");
} else{
printError("emailErr", "");
emailErr = false;
}
}
if(title == "Select") {
printError("titleErr", "Please select your title");
} else {
printError("titleErr", "");
titleErr = false;
}
if((nameErr || emailErr || titleErr) == true) {
return false;
}
I expect it to provide an error message when the submit button is clicked. Link to my entire code https://jsfiddle.net/lizzyblue02/spmygzdw/
Overall it works fine, I just had to make a couple of changes.
Your input type="submit" submits the form every time it is clicked, even if the form validation returned errors. I changed it in input type="button", and the form is now submitted using JavaScript and only if the form validation returned no error.
As mentioned in the above comment, you also needed to close the validate function later in the function.
You can see result here.
I am trying to validate two input fields in my form using javascript. But my functions checks only one value for null or empty string. It submits the form if the other value is empty. Why?
function checkFieldEmpty()
{
var a=document.forms["verifyURN"]["urnNumber"].value;
var b=document.forms["verifyURN"]["urnDate"].value;
if (a==null || a=="", b==null || b=="") //b field validates here, not a..?
{
return false;
}
return true; //function returns true even if a is empty..?
}
//Below function is called when submit button pressed in my form
function verifyURN()
{
if(checkFieldEmpty())
{
document.verifyURN.rDoAction.value = "<%=Constant.myPage%>";
document.verifyURN.submit();
}
else{
alert("Mandatory fields empty");
return false;
}
}
...
<form name="verifyURN"...
try add
alert(a.length);
alert(b.length);
before your
if (a==null || a=="", b==null || b=="") //b field validates here, not a..?
{
and you will have a better picture of what criteria to check.
I am checking whether someone has selected a property type (house/flat) and then checking the sub list of either houses or flats to see whether they have selected a sub-type of there selected property (ie. house -> semi-detached etc.)
The house validation is working fine, the flats validation isn't however even though their syntax is the same, I feel like I'm missing something really obvious here.
jsfiddle: http://jsfiddle.net/dyates88/vCmnW/iddle
jQuery:
valid = true;
if (index === 2) {
if (!$("#htype div").hasClass("selected")) {
alert("Please select a Property type!");
valid = false;
} else if ($("#htype div").attr('sel') == 'true' && $("#htype div").attr('id') == 'flat' && !$("#flats div").hasClass("selected")) {
alert("Please Select a flat type!");
valid = false;
} else if ($("#htype div").attr('sel') == 'true' && $("#htype div").attr('id') == 'house' && !$("#houses div").hasClass("selected")) {
alert("Please Select a house type!");
valid = false;
}
Now the form is one form, here is the new broken code
<script language="javascript" type="text/javascript">
function verifyIt(){
if((document.form1.baseline_08.value != "" && Number(document.form1.baseline_08.value) && document.form1.baseline_08.value != "-1")){
if((document.form1.baseline_09.value != "" && Number(document.form1.baseline_09.value) && document.form1.baseline_09.value != "-1")){
document.form1.submit();
return true;
}else{
alert("Please select how old you were when you started smoking every day.");
return false;
}
}
function submit2(){
document.form1.direction.value = "back";
document.form1.submit();
}
</script>
Now the verify doesnt work at all. I just dont see what is wrong with this now.
The problem I am having is the form1 is the only one being recognized. I believe it is because of my if statement structure. Basicly I only get the return from the form1. What is wrong with my js?
I believe it is because of my if statement structure.
Yes, you are always returning after checking form1. However, you cannot submit multiple forms at a time, so you should combine them into one. Then use
function verifyIt() {
if(!(document.form.baseline_08.value == "" && Number(document.form.baseline_08.value) && document.form.baseline_08.value != "-1")){
alert("Please select how old you were when you started smoking every day.");
return false;
}
if(!(document.form.baseline_09.value != "" && Number(document.form.baseline_09.value) && document.form.baseline_09.value != "-1")){
alert("Please select when you would smoke after waking up.");
return false;
}
document.form.submit();
}
I have the following validation code on my .asp webpage. I also need to validate the txtndc textbox so that the data entered looks like this. 00000-0000 Currently they can enter any info in this textbox. The entry should always be 5 numbers a dash and 4 numbers. Any help would be greatly appreciated.
function form_onsubmit() {
if (form1.txtdrug.value == "")
{
alert("Drug Name Needed");
return false;
}
if (form1.txtndc.value == "")
{
alert("NDC Number Needed");
return false;
}
if (form1.txtndc.value != "" && form1.txtdrug.value != "")
{
alert("Drug was Successfully entered into the database, hit enter to continue.");
return true;
}
}
How would the syntax be written. The textbox i am trying to check is
<input type="text" name="txtndc" size="35">.
I am not sure how to enter your code below above into my page. Please Help
Using Javascript regex#test function.
x = /^[0-9]{5}-[0-9]{4}$/;
console.log(x.test("12113-1234"));
>>> x = /^[0-9]{5}-[0-9]{4}$/; console.log(x.test("00000-0004"));
true
Demo: http://jsbin.com/axikiv/1/edit