Validate form when press "buy it now" button

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
16374 2440 3189

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()"

 

Hire us | Pass Core Web Vital | B2B Wholesale Experts | Claim Your Free Website Review |
Connect with Us: WhatsApp | Skype: oscprofessionals-87 | Email: pallavi@oscprofessionals.com |
Custom Shopify SolutionsPrivate Apps, Theme Customization & SEO | Digital Marketing |
OSCP Apps: Discount Suite | Wholesale App | Bundle & Upsell | Shipping Discount | and more...
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