View Single Post
  #5  
Old 01-09-2013, 04:49 PM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default Re: Make Custom Text Field Required???

Well we need to get the value of the input box first. So look at the source of the page, using Chrome right click on the input box and "inspect element".

Now you should see something similar to this:
<input id="po455" type="text" name="product_options[455]">

I'm sure you could do this a number of ways, but I am going to just use the input's id in order to reference this box in my JS.

var option = $('#po455').val();

This declares a variable, and names it "option", then sets it using a jQuery method of obtaining the value of the input by referencing it's id. So now we can refer to that value simply as option in our code.

Consider this code:

if (option != "funky") {
window.alert(option);
return false;
}

So now it is checking if the value of the input box matches the string "funky", if it doesn't then it alerts whatever the current value of the box is. If it is blank you should see nothing in the alert box, if you type "Whalah!" into the box it should alert that to you. The bottom line is that if it matches what you put between the braces () then it will perform what you put within the curly braces {}. And then it returns false, and the form is not submitted.

So to check if the box is empty, we would try something like:

if(option == '' or option == null) {
alert("Type something, you dope!");
return false;
}

__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote