|
1 |
| -// App.js |
| 1 | +import React, { useState } from "react"; |
| 2 | +import validator from "validator"; |
2 | 3 |
|
3 |
| -import React, { useState } from "react"; |
4 |
| -import validator from 'validator' |
| 4 | +const App = () => { |
| 5 | + const [errorMessage, setErrorMessage] = useState(""); |
| 6 | + const [flag, setFlag] = useState(false); |
| 7 | + const [credit, setCredit] = useState(""); |
5 | 8 |
|
6 |
| -const App = () => { |
| 9 | + const validateCreditCard = (value) => { |
| 10 | + if (validator.isCreditCard(value)) { |
| 11 | + setErrorMessage(" Valid Credit Card Number"); |
| 12 | + setFlag(true); |
| 13 | + } |
| 14 | + else if(value===''){ |
| 15 | + setErrorMessage(""); |
| 16 | + } |
| 17 | + else { |
| 18 | + setErrorMessage(" Enter a valid Credit Card Number!"); |
| 19 | + setFlag(false); |
| 20 | + } |
| 21 | + }; |
7 | 22 |
|
8 |
| -const [errorMessage, setErrorMessage] = useState('') |
9 |
| - |
10 |
| -const validateCreditCard = (value) => { |
11 |
| - |
12 |
| - if (validator.isCreditCard(value)) { |
13 |
| - setErrorMessage('Valid CreditCard Number') |
14 |
| - } else { |
15 |
| - setErrorMessage('Enter valid CreditCard Number!') |
16 |
| - } |
17 |
| -} |
| 23 | + return ( |
| 24 | + <div |
| 25 | + style={{ |
| 26 | + display: "flex", |
| 27 | + flexDirection: "column", |
| 28 | + alignItems: "center", |
| 29 | + fontFamily: "Arial, sans-serif", |
| 30 | + padding: "40px", |
| 31 | + backgroundColor: "#f9f9f9", |
| 32 | + height: "100vh", |
| 33 | + }} |
| 34 | + > |
| 35 | + <div |
| 36 | + style={{ |
| 37 | + background: "#101b30", |
| 38 | + padding: "30px 40px", |
| 39 | + borderRadius: "10px", |
| 40 | + boxShadow: "0 4px 10px rgba(0,0,0,0.1)", |
| 41 | + maxWidth: "400px", |
| 42 | + width: "100%", |
| 43 | + }} |
| 44 | + > |
| 45 | + <h2 style={{ color: "#fff", marginBottom: "20px" }}> |
| 46 | + Credit Card Validator |
| 47 | + </h2> |
18 | 48 |
|
19 |
| -return ( |
20 |
| - <div style={{ |
21 |
| - marginLeft: '200px', |
22 |
| - }}> |
23 |
| - <pre> |
24 |
| - <h2>Validating CreditCard in ReactJS</h2> |
25 |
| - <span>Enter CreditCard: </span><input type="text" |
26 |
| - onChange={(e) => validateCreditCard(e.target.value)}></input> <br /> |
27 |
| - <span style={{ |
28 |
| - fontWeight: 'bold', |
29 |
| - color: 'red', |
30 |
| - }}>{errorMessage}</span> |
31 |
| - </pre> |
32 |
| - </div> |
33 |
| -); |
34 |
| -} |
| 49 | + <label style={{ marginBottom: "10px", display: "block", color: "#aaa" }}> |
| 50 | + Enter Credit Card Number: |
| 51 | + </label> |
35 | 52 |
|
36 |
| -export default App |
| 53 | + <input |
| 54 | + type="password" |
| 55 | + value={credit} |
| 56 | + onChange={(e) => { |
| 57 | + const newValue = e.target.value; |
| 58 | + setCredit(newValue); |
| 59 | + validateCreditCard(newValue); |
| 60 | + }} |
| 61 | + placeholder="•••• •••• •••• ••••" |
| 62 | + style={{ |
| 63 | + width: "100%", |
| 64 | + padding: "10px", |
| 65 | + fontSize: "16px", |
| 66 | + border: "1px solid #ccc", |
| 67 | + borderRadius: "6px", |
| 68 | + outline: "none", |
| 69 | + marginBottom: "15px", |
| 70 | + }} |
| 71 | + /> |
| 72 | + |
| 73 | + <div |
| 74 | + style={{ |
| 75 | + fontWeight: "600", |
| 76 | + color: flag ? "#44ed23" : "red", |
| 77 | + minHeight: "24px", |
| 78 | + }} |
| 79 | + > |
| 80 | + {errorMessage} |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +export default App; |
0 commit comments