Copy link to clipboard
Copied
I have a text box called Part_Number. If the part number starts with a "C", then I want my Validation_Qty text box be a requirement to be filled in. If there's anything else in the Part_Number box, nothing else matters. There are no requirements for any other part numbers. How would I go about dong this? Here are my 2 text boxes:
<input type="text" name="Part_Number" size="30">
<input type="text" name="Validation_Qty" size="12" validate="integer" required="no" message="You must enter a valid number in the Validation Quantity field">
Thanks.
Andy
Copy link to clipboard
Copied
Assuming that you want to use CFFORM / CFINPUT validation; you can create your own JavaScript to perform your custom validation and include it using the onValidate attribute of CFINPUT.
Copy link to clipboard
Copied
Bob,
I added this javascript to my page, but this only stops the page from adding if the Validation Qty. box is not filled in at all. It does not worry if the part number box starts with a C or not. How do I do a wildcard in Javascript that says if the Part number starts with a C, then only require that the Validation Qty. be filled in then? I thought this would work: /C^$/ but it doesn't.
<SCRIPT LANGUAGE="JavaScript">
function verify() {
var themessage = "Please enter ";
if (document.AddItem.Part_Number.value !== /C^$/ && document.AddItem.Validation_Qty.value == "") {
themessage = themessage + "Validation Qty.";
}
if (themessage == "Please enter ") {
document.AddItem.submit();
}
else {
alert(themessage);
return false;
}
}
</script>
Andy
Copy link to clipboard
Copied
Here a sample which uses CFINPUT validation. You might adpat this code for your validation.
See: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7a7c.html
<html>
<head>
<title>Test Custom JavaScript</title>
</head>
<body>
<cfform id="inputForm" name="inputForm" action="index.cfm" method="post">
Part Number <input type="text" name="Part_Number" size="30"><br>
Quantity <cfinput type="text" name="ValidationQty" size="12" validate="integer,range" range="1" required="no" onValidate="validationQtyCustomValidation" message="You must enter a valid number in the Validation Quantity field"><br>
<input type="submit" value="Submit">
</cfform>
<script type="text/javascript">
function validationQtyCustomValidation(formObject, inputObject, objectValue) {
//lookup part number
var partNum = document.forms["inputForm"]["Part_Number"].value;
if(partNum.length > 0 && partNum.substring(0,1) == "C")
{
if(objectValue.length < 1)
{
return false;
}
}
return true;
}
</script>
</body>
</html>
Message was edited by: JR "Bob" Dobbs Added HTML sample.
Copy link to clipboard
Copied
Bob,
This code does not do anything different. It still allows it to go through if a C is typed in or not. This is what I have:
<script type="text/javascript">
function validation_QtyCustomValidation(formObject, inputObject, objectValue) {
//lookup part number
var partNum = document.forms["AddItem"]["Part_Number"].value;
if(partNum.length > 0 && partNum.substring(0,1) == "C")
{
if(objectValue.length < 1)
{
return false;
}
}
return true;
}
</script>
<input type="text" name="Part_Number" size="30">
<input type="text" name="Validation_Qty" size="12" validate="integer,range" range="1" onValidate="validationQtyCustomValidation" message="You must enter a valid number in the Validation Quantity field">
I had to remove the required="no" in the text box for the Validation_Qty field because it does not go through at all if nothing is entered into this box. Why doesn't this script stop if a C is in the box?
Andy
Copy link to clipboard
Copied
Can your clarify your requirements?
The code sample I provided should require a quantity (input name "Validation_Qty" ) when the value of part number (input name "Part_Number" ) starts with "C" or there is content is the quantity text box. If the part number is blank or does not start "C" and the quantity is empty the quantity field is not validated.
Copy link to clipboard
Copied
Bob,
If I undertand you correctly, I think you understand what I want. Basically, if the Part_Number starts with a C and the Validation_Qty box is empty and you click add, then pop up a window that says the Validation_Qty must be filled in. I don't have any other requirements. I copied the code you sent, but it does not pop up a window stopping me and telling me to enter a Validation_Qty if the Part_Number starts with a C.
andy
Copy link to clipboard
Copied
Do you know what js code will pop up a window? If so, can you look at the code and determine the best place for it?
Copy link to clipboard
Copied
I have this script that pops up a window with a message that says, "Please enter Validation Qty." This is if the Part_Number has something in it and the Validation_Qty is blank. I tried requiring the C, but it did not work.
<SCRIPT LANGUAGE="JavaScript">
function verify() {
var themessage = "Please enter ";
if (document.AddItem.Part_Number.value !== /C^$/ && document.AddItem.Validation_Qty.value == "") {
themessage = themessage + "Validation Qty.";
}
if (themessage == "Please enter ") {
document.AddItem.submit();
}
else {
alert(themessage);
return false;
}
}
</script>
My Text boxes look like this:
<input type="text" name="Part_Number" size="30">
<input type="text" name="Validation_Qty" size="12" validate="integer" message="You must enter a valid number in the Validation Quantity field">
The Add Button looks like this:
<input type="button" value="Add" onclick="verify();">
Andy
Copy link to clipboard
Copied
So you do know the code for a pop-up window. Good. Now, if you look at Bob Dobb's code, where do you think the best spot for that command to be added?
Copy link to clipboard
Copied
Dan,
The only thing I can see that makes the pop-up window show up with the javascript code is when I have the buttton at the bottom like this:
<input type="button" value="Add" onclick="verify();">
If I try and adapt Bob's code into this javascript, no window pops up anymore unless I change the Validation_Qty text box to the code Bob sent and required="no" is in this, and if I change the button at the bottom to this:
<input type="submit" value="Add">
This is the Validation_Qty text box:
<cfinput type="text" name="ValidationQty" size="12" validate="integer,range" range="1" required="no" onValidate="validationQtyCustomValidation" message="You must enter a valid number in the Validation Quantity field">
Even if I put this: (formObject, inputObject, objectValue) into the Verify() in the javascript code and in the button at the bottom, nothing pops up then either. Or even if I change Verify to:
validationQtyCustomValidation
it doesn't pop up.
So basically, the only way I get a pop up window is with my orignal javascript code. Or if I change the button at the bottom to a submit button rather than just a button, and required="no" is in the Validaton_Qty text box.
I'm really confused. I even put Bob's code on it's own page and tried making it work there and it only popped up a window if that required="no" text was in the Validation_Qty text box code. If I remove that, it doesn't do anything. How can I make this work? I really don't understand javascript at all.
Andy
Copy link to clipboard
Copied
Back to basics.
The js command that creates a pop-up window is
alert("text goes here");
Next, Reply number 3 in this thread, by Bob Dobbs, has a script that has very good logic to check whether or a part number starts with C. However, that script does not have an alert command.
If you put Bob's code into your page, and add an alert command in the proper place, you should be in good shape. I'll leave it to you to determine the best place for that command.
Copy link to clipboard
Copied
Dan,
I don't even know the basics of Javascript. I never had any training or classes on Javascript, so I'm just copying and pasting things into multiple different places until something works. I was able to get this to display a pop up window if a "C" is entered into the Part_Number field and the Validation_Qty box is empty, but if I type a number into the Validation_Qty box, and hit the Add button, it doesn't do anything. Or if I type anything else into the Part_Number field and hit the add button, it doesn't do anything. What am I doing wrong now?
<script type="text/javascript">
function validationQtyCustomValidation(formObject, inputObject, objectValue) {
//lookup part number
var partNum = document.forms["AddItem"]["Part_Number"].value;
if(partNum.length > 0 && partNum.substring(0,1) == "C")
{
alert("You must enter a valid number in the Validation Quantity field");
}
{
if(objectValue.length < 1)
{
return false;
}
}
return true;
}
</script>
Andy
Copy link to clipboard
Copied
When I was in that situation, I bought the book, Teach Yourself Javascript in 24 Hours and did every tutorial. It met my needs.
I also know that webmonkey.com has some javascript tutorials. Not having taken them, I have no opinion of them.
Copy link to clipboard
Copied
Dan Bracuk wrote:
I also know that webmonkey.com has some javascript tutorials. Not having taken them, I have no opinion of them.
I did use the webmonkey tutorials back in the day (~2000) and found them to be very helpful.
Copy link to clipboard
Copied
Finally, here's the answer to test if a C and then a number is entered with having javascript stop from submitting the info. if this condition is not met:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function verify() {
var PartNum=document.AddItem.Part_Number.value;
var regularExpression = new RegExp(/[cC][0-9]/); //regular expression to check for a letter C followed by a number
if(regularExpression.test(PartNum)&& document.AddItem.Validation_Qty.value == "" ) { //this will return true if the input passes the regular expression
alert("You must enter a valid number in the Validation Quantity field");
}
else
{
document.AddItem.submit();
}
}
// End -->
</script>
Andy