Solved

Validate form when press "buy it now" button

Percentage
Shopify Partner
4 2 0

Hello,

 

On product page, I have "upload file" field and "note" field (It's custom field.), They are required field.
I use getElementsByClassName("shopify-payment-button__button") and add onclick by javascript

for validate 2 fields ("upload file" field and "note" field).

I use preventDefault() and stopImmediatePropagation()
It is not work, 
I cannot stop for go to checkout page.

 

What do I do validate "upload file" field and "note" field before before "buy it now" button works ?

Accepted Solution (1)

Percentage
Shopify Partner
4 2 0

This is an accepted solution.

I cannot validate for "buy it now" button.

before when I upload file, I hide "buy it now" button
After upload success, I show "buy it now" button

View solution in original post

Replies 3 (3)

oscprofessional
Shopify Partner
15905 2378 3087

Hi, 

1. For file input create,

 

 

<input type="file" id="file" />

 

 

2. Inside script tag create function 

 

 

function ValidateFileInput() {
            var fileInput = document.getElementById('file');

 

 

along with this you need to check file(allowed extensions like .jpg, .jepg, .png, etc)
3. 

<input type="text" name="firstname" id="firstname" />

 Check validation for, 

 

 

var fname = document.getElementById('firstname');
if (fileInput.file && fname) {
// Your code goes here
   } // clsoe if statement
}  // function

 

4. Buy Now button 

  onclick="return ValidateFileInput()"

 

Get pass your Store Core Web Vital Free Speed Optimization Audit, Chat on WhatsApp | Skype : oscprofessionals-87 | Email: pallavi@oscprofessionals.com | Hire us | Guaranteed Site Speed Optimization | Website Free Audit | Shopify Theme Customization | Build Shopify Private App | Shopify SEO | Digital Marketing | Oscp Upsell & Cross sell App : Free | Oscp Sales & Volume Discount App : Free | Custom Pricing Wholesale App : Free
Gfamtech
Visitor
1 0 0

where to paste this code . i tried to pace this code in poduct.liquid buy it is not working pls help to slove this . for my custom field i need to validate for buy it now button 

 

Percentage
Shopify Partner
4 2 0

This is an accepted solution.

I cannot validate for "buy it now" button.

before when I upload file, I hide "buy it now" button
After upload success, I show "buy it now" button