Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ Shopping cart Quantity Component


๐Ÿ“š Shopping cart Quantity Component


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Note, This post is for beginners

In this post we see how to design a e-commerce shopping app quantity / qty component with regex validation.

This is the basic page and basic functions, hope you know them already.

Image description


import React, { useState } from 'react';
import {
  View,
  Text,
  StyleSheet,
  TextInput,
  TouchableOpacity,
} from 'react-native';

export default function App() {
  const [qty, setQty] = useState(0);

  return (
    <View style={styles.container}>
      <View style={styles.qtyLine}>
        <TouchableOpacity
          onPress={() => qty < 100 && setQty(qty + 1)}
          style={styles.btn}>
          <Text style={styles.btnText}>{'+'}</Text>
        </TouchableOpacity>
        <TextInput
          keyboardType={'number-pad'}
          maxLength={3}
          style={styles.qtyInput}
          value={'' + qty}
          onChangeText={(txt) => {
            let num = Number(txt);
            if (!isNaN(num) && num > 0 && num < 101) {
              setQty(num);
            } else setQty(0);
          }}
        />
        <TouchableOpacity
          onPress={() => qty > 0 && setQty(qty - 1)}
          style={styles.btn}>
          <Text style={styles.btnText}>{'-'}</Text>
        </TouchableOpacity>
        <TouchableOpacity
          onPress={() => setQty(0)}
          style={[styles.btn, { marginLeft: 5 }]}>
          <Text style={styles.btnText}>{'x'}</Text>
        </TouchableOpacity>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'snow',
    alignItems: 'center',
    justifyContent: 'center',
    padding: 10,
    paddingTop: 20,
  },
  qtyLine: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
  },
  qtyInput: {
    marginHorizontal: 10,
    borderWidth: 0.5,
    borderColor: 'dodgerblue',
    padding: 5,
    textAlign: 'center',
    width: '20%',
  },
  btn: {
    backgroundColor: 'dodgerblue',
    paddingVertical: 5,
    paddingHorizontal: 10,
    borderRadius: 3,
  },
  btnText: {
    color: 'white',
  },
});

Number(txt)
// also converts empty string and blank space(s) as 0

At present in TextInput onChangeText event we are checking 3 conditions. Checking a text is a number, number is > 0 and number is < 101. But this can be simplified in one step by using regex validation.

Lets touch the TextInput component. In onChangeText event we are using regex to validate the given string is a number and the value is between 1 to 100.

TextInput with RegEx pattern

const qtyRgPtn = /^([0-9][0-9]?|100)$/;
. . .
<TextInput onChangeText={txt => 
qtyRgPtn.test(txt) && setQty(Number(txt))} />

so simple isn't it?

Now in addition to that we are showing a warning message for short time when the user inputs invalid qty.

const qtyRgPtn = /^([0-9][0-9]?|100)$/;
export default function App() {
  const [qty, setQty] = useState(0);
  const [qtyError, setQtyError] = useState('');
. . .

  <TextInput 
   onChangeText={(txt) => {
            /*
            let num = Number(txt);
            if (!isNaN(num) && num > 0 && num < 101) {
              setQty(num);
            } else {
              setQty(0);
            }
            */
            if (qtyRgPtn.test(txt)) setQty(Number(txt));
            else {
              setQty(0);
              if (!txt || !txt.trim().length) {
                setQtyError('Qty should be in between 1-100');
                setTimeout(() => setQtyError(''), 2000);
              }
            }
          }}
  />
. . . 
{/* final child of container */}
      {qtyError ? (
        <Text style={{ color: 'salmon', fontSize: 12, marginTop: 5 }}>
          {qtyError}
        </Text>
      ) : null}

Good! we are done :)

Image description

Hope this post will be useful. Source code here. Thank you.

...



๐Ÿ“Œ Shopping cart Quantity Component


๐Ÿ“ˆ 55.8 Punkte

๐Ÿ“Œ OpenCA bis 1.5.6.4 system/library/cart.php Cart::getProducts quantity erweiterte Rechte


๐Ÿ“ˆ 49.27 Punkte

๐Ÿ“Œ OpenCA up to 1.5.6.4 system/library/cart.php Cart::getProducts quantity XML External Entity


๐Ÿ“ˆ 49.27 Punkte

๐Ÿ“Œ OpenCA bis 1.5.6.4 system/library/cart.php Cart::getProducts quantity erweiterte Rechte


๐Ÿ“ˆ 49.27 Punkte

๐Ÿ“Œ CVE-2006-4214 | Zen Cart add_cart quantity sql injection (XFDB-28393 / Nessus ID 22233)


๐Ÿ“ˆ 36.6 Punkte

๐Ÿ“Œ CVE-2024-3003 | code-projects Online Book System 1.0 /cart.php quantity/remove sql injection


๐Ÿ“ˆ 36.6 Punkte

๐Ÿ“Œ Avactis Shopping Cart cart.php prod_id sql injection


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Craig Dansie Dansie Shopping Cart 3.0.4 URL cart.pl env/db/vars privilege escalation


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Comersus Open Technologies Comersus Cart Shopping Cart message cross site scripting


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Comersus Open Technologies Comersus Cart Shopping Cart comersus_optaffiliateregistrationexec.asp idProduct sql injection


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Online shopping cart Zen Cart patches critical XSS flaws


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Online shopping cart Zen Cart patches critical XSS flaws


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Craig Dansie Dansie Shopping Cart 3.0.4 Form cart.pl information disclosure


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Craig Dansie Dansie Shopping Cart 3.0.4 Form cart.pl information disclosure


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Introducing Cart: Simplifying Shopping Cart Management for Laravel


๐Ÿ“ˆ 34.9 Punkte

๐Ÿ“Œ Tailwind CSS Shopping Cart Component Examples


๐Ÿ“ˆ 31.86 Punkte

๐Ÿ“Œ CVE-2022-46806 | Cart All In One for WooCommerce Plugin up to 1.1.10 on WordPress Cart Changes cross-site request forgery


๐Ÿ“ˆ 25.33 Punkte

๐Ÿ“Œ Open Solution Quick.Cart 2.0 quick.cart config[db_type] directory traversal


๐Ÿ“ˆ 25.33 Punkte

๐Ÿ“Œ Open Solution Quick.Cart 2.0 quick.cart unknown vulnerability


๐Ÿ“ˆ 25.33 Punkte

๐Ÿ“Œ Low CVE-2020-28456: S-cart S-cart


๐Ÿ“ˆ 25.33 Punkte

๐Ÿ“Œ Low CVE-2020-28457: S-cart S-cart


๐Ÿ“ˆ 25.33 Punkte

๐Ÿ“Œ [webapps] CSE Bookstore 1.0 - 'quantity' Persistent Cross-site Scripting


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ #0daytoday #CSE Bookstore 1.0 - (quantity) Persistent Cross-site Scripting Vulnerability [#0day #Exploit]


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ Report: Quality, not quantity, is the hallmark of the latest waves of phishing attacks


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ Lessons From the Cold War: How Quality Trumps Quantity in Cybersecurity


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ Quantity vs Quality in Coffee Data


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ The clock is ticking on this limited-quantity RTX 4090 gaming desktop deal โ€” it's $800 off if you can claim it in time


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ โ€œML-Everythingโ€? Balancing Quantity and Quality in Machine Learning Methods for Science


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ #145 Quality Over Quantity - This Week in GNOME


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ Square Enix shifting from "quantity to quality" and be more multi-platform


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ AI Will Increase the Quantityโ€”and Qualityโ€”of Phishing Scams


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ CVE-2024-2151 | SourceCodester Online Mobile Management Store 1.0 Product Price quantity logic error


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ Networking Connections: Is it Quality or Quantity?


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ AI Will Increase the Quantityโ€”and Qualityโ€”of Phishing Scams


๐Ÿ“ˆ 23.94 Punkte

๐Ÿ“Œ [webapps] - Joomla! Component aWeb Cart Watching System for Virtuemart 2.6.0 - SQL Injection


๐Ÿ“ˆ 22.29 Punkte











matomo